The API I'm trying to call requires a POST with an empty body. I'm using the WCF Web API HttpClient, and I can't 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.
-
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– Darrel Miller2011-10-27 23:18:20 +00:00Commented Oct 27, 2011 at 23:18
-
Possible duplicate of How do I set up HttpContent for my HttpClient PostAsync second parameter?Michael Freidgeim– Michael Freidgeim2018-04-16 02:25:22 +00:00Commented Apr 16, 2018 at 2:25
-
3@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– Ryan Rinaldi2018-06-19 16:04:21 +00:00Commented Jun 19, 2018 at 16:04
-
2"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 meta.stackexchange.com/questions/147643/… If you agree that it requires clarification please vote on meta.stackexchange.com/questions/281980/…Michael Freidgeim– Michael Freidgeim2018-06-19 22:24:03 +00:00Commented Jun 19, 2018 at 22:24
Add a comment
|
8 Answers
Use StringContent or ObjectContent which derive from HttpContent or you can use null as HttpContent:
var response = await client.PostAsync(requestUri, null);
9 Comments
dan
It looks like this is only in .NET framework 4.5? msdn.microsoft.com/en-us/library/…
Alexander Zeitler
It will ship with WCF Web API but I think some of the "good parts" will make it into the framework itself.
tugberk
Why 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?Owain Williams
You can use
null as the HttpContent, this will send no body in the request, e.g. var response = await client.PostAsync(requestUri, null);Andi
Assembly
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!. |
Did this before, just keep it simple:
Task<HttpResponseMessage> task = client.PostAsync(url, null);
1 Comment
David Ebbo
This is cleaner than accepted answer, and should be voted up.
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.
1 Comment
O. R. Mapper
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.If you wish to avoid 'null' and make your intentions clear, as do I, you can override the StringContent class:
private sealed class DeliberatelyEmptyContent : StringContent
{
public DeliberatelyEmptyContent() : base(string.Empty)
{
}
}
public async Task ActivateUser(string xflowUserId)
{
var httpclient = _httpClientFactory.CreateClient(_namedHttpClientName);
string activateUrl = $"/User/{xflowUserId}/Activate";
_ = await httpclient.PutAsync(activateUrl, new DeliberatelyEmptyContent());
}
Comments
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;
}
Comments
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);
3 Comments
Ryan Rinaldi
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.
Ivan G.
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
Ryan Rinaldi
I'm using HttpClient, not HttpWebRequest. Using StringContent with an empty string worked.