Put empty object in REST API via HttpClient

The API I'm trying to call requires me to execute POST, but with an empty body. I am new to using the WCF Web API HttpClient API, and I can not find the write code that will make the message with an empty body. I find links to some HttpContent.CreateEmpty () method, but I donโ€™t think it is for the HttpClient Web API API code, since I cannot find this method.

+84
c # wcf-web-api
Oct 26 '11 at 19:13
source share
4 answers

Use StringContent or ObjectContent that come out of HttpContent , or you can use null as HttpContent (see this comment ).

+77
Oct 26 2018-11-11T00:
source
โ€” -

Did it before, just simple:

 Task<HttpResponseMessage> task = client.PostAsync(url, null); 
+80
May 12 '17 at 11:34
source

Found that:

 Task<HttpResponseMessage> task = client.PostAsync(url, null); 

Adds zero to the request body, which failed on WSO2. Replaced by:

 Task<HttpResponseMessage> task = client.PostAsync(url, new {}); 

And it worked.

+3
Sep 07 '17 at 22:53 on
source

I think this is automatic if your web method has no parameters or all of them fit into the URL pattern.

For example, this declaration sends an empty body:

  [OperationContract] [WebGet(UriTemplate = "mykewlservice/{emailAddress}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] void GetStatus(string emailAddress, out long statusMask); 
-6
Oct. 26 '11 at 19:23
source



All Articles