The API I'm trying to call requires a POST with an empty body. I'm new to using the WCF Web API HttpClient, and I can't seem to find the right code that will post with an empty body. I found references to some HttpContent.CreateEmpty() method, but I don't think it’s for the Web API HttpClient code since I can't seem to find that method.
Asked
Active
Viewed 1e+01k times
186
-
HttpContent.CreateEmpty was from the old HttpClient prototype that was part of REST Starter kit. Unfortunately there is no equivalent in the new HttpClient. – Darrel Miller Oct 27 '11 at 23:18
-
Possible duplicate of [How do I set up HttpContent for my HttpClient PostAsync second parameter?](https://stackoverflow.com/questions/18971510/how-do-i-set-up-httpcontent-for-my-httpclient-postasync-second-parameter) – Michael Freidgeim Apr 16 '18 at 02:25
-
1@MichaelFreidgeim If there was a hole in the space time continuum and somehow 2013 came before 2011, then yes it is a possible duplicate. – Ryan Rinaldi Jun 19 '18 at 16:04
-
1"Possible duplicate" is a way to clean-up - to close similar questions and keep one with the best answers. The date is not essential. See http://meta.stackexchange.com/questions/147643/should-i-vote-to-close-a-duplicate-question-even-though-its-much-newer-and-ha If you agree that it requires clarification please vote on http://meta.stackexchange.com/questions/281980/add-clarification-link-to-possible-duplicate-automated-comment – Michael Freidgeim Jun 19 '18 at 22:24
5 Answers
224
Use StringContent or ObjectContent which derive from HttpContent or you can use null as HttpContent:
var response = await client.PostAsync(requestUri, null);
Soviut
- 83,904
- 44
- 175
- 239
Alexander Zeitler
- 10,348
- 8
- 66
- 105
-
It looks like this is only in .NET framework 4.5? http://msdn.microsoft.com/en-us/library/system.net.http.stringcontent(v=VS.110).aspx – dan Dec 28 '11 at 03:54
-
It will ship with WCF Web API but I think some of the "good parts" will make it into the framework itself. – Alexander Zeitler Dec 28 '11 at 07:33
-
1Why isn't there any overload methods which does not require a `HttpContent` class? Should we at least provide something (even an empty string) to make a http post? – tugberk Jan 30 '12 at 10:28
-
84You can use `null` as the `HttpContent`, this will send no body in the request, e.g. `var response = await client.PostAsync(requestUri, null);` – Owain Williams Aug 05 '15 at 13:09
-
@OwainWilliams said the same as the original answer, but this comment got way more upvotes as it's straight to the point, for me, boosted the confidence ;) – Deepak Jan 20 '21 at 16:25
-
7Assembly `System.Net.Http` in version 5.0.0.0 has still no nullable `HttpContent` parameter, so `null` should be not allowed. But it (still) seems to work. I could pass `null!`. – Andi Feb 17 '21 at 17:36
-
C# 8.0 introduced [nullable reference types](https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references), before that _all_ reference types could be `null`. I assume the public `HttpContent` parameters in the [`HttpClient`](https://github.com/dotnet/runtime/blob/v5.0.5/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.cs) have not been explicitly declared as non-nullable to maintain backwards compatibility with older C# versions. I can't see anything in the code that implies the `HttpContent` cannot be `null`, which I interpret to mean that `null` _**is**_ allowed. – Owain Williams Apr 26 '21 at 11:36
124
Did this before, just keep it simple:
Task<HttpResponseMessage> task = client.PostAsync(url, null);
Ogglas
- 50,115
- 30
- 272
- 333
8
Have found that:
Task<HttpResponseMessage> task = client.PostAsync(url, null);
Adds null to the request body, which failed on WSO2. Replaced with:
Task<HttpResponseMessage> task = client.PostAsync(url, new {});
And worked.
Erik Philips
- 51,408
- 11
- 123
- 146
Ryan Tuck
- 81
- 1
- 2
-
I cannot confirm this finding (but I am not sure my test was totally adequate). When I POST to one of my own APIs with a `null` content and look at the content found in the `HttpRequestMessage`, I seem to be getting a length of zero bytes. – O. R. Mapper Nov 18 '19 at 12:03
2
To solve this problem, use this example:
using (var client = new HttpClient())
{
var stringContent = new StringContent(string.Empty);
stringContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
var response = client.PostAsync(url, stringContent).Result;
var result = response.Content.ReadAsAsync<model>().Result;
}
abolfazl mousavi
- 81
- 1
- 3
-6
I think it does that automagically if your web method has no parameters or they all fit into URL template.
For example this declaration sends empty body:
[OperationContract]
[WebGet(UriTemplate = "mykewlservice/{emailAddress}",
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
void GetStatus(string emailAddress, out long statusMask);
Ivan G.
- 4,812
- 2
- 35
- 62
-
I'm trying to SEND an empty body. The HttpClient.Post() method requires an URI and a HttpContent object. I'm not what to pass as the HttpContent when I don't want to send anything. – Ryan Rinaldi Oct 26 '11 at 19:27
-
So you're not using WCF. That's even easier: ... HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://..."); request.Method = "POST"; HttpWebResponse respose = (HttpWebResponse)request.GetResponse(); ... you result in response – Ivan G. Oct 26 '11 at 19:30
-
1I'm using HttpClient, not HttpWebRequest. Using StringContent with an empty string worked. – Ryan Rinaldi Oct 27 '11 at 16:11