Post an empty body to REST API via HttpClient
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.
Did this before, just keep it simple:
Task<HttpResponseMessage> task = client.PostAsync(url, null);
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;
}
Use StringContent
or ObjectContent
which derive from HttpContent
or you can use null
as HttpContent
:
var response = await client.PostAsync(requestUri, null);