When I'm using DeleteAsync
function in HttpClient
(System.Net.Http) and retrieve the content with Content.ReadAsStringAsync()
I always get null
returned.
I've tried the same with GET
, POST
and PUT
- and they always return some result.
Here is my code:
HttpClient _client = new HttpClient();
_client.BaseAddress = new Uri("http://httpbin.org/");
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = _client.DeleteAsync("/delete").Result;
string res = await response.Content.ReadAsStringAsync();
return await JsonConvert.DeserializeObjectAsync<T>(res);
I always get null
returned.
However, all of this works:
GET:
HttpResponseMessage response = _client.GetAsync("/get").Result;
string res = await response.Content.ReadAsStringAsync();
return await JsonConvert.DeserializeObjectAsync<T>(res);
POST:
HttpResponseMessage response = _client.PostAsync("/post", new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json")).Result;
string res = await response.Content.ReadAsStringAsync();
return await JsonConvert.DeserializeObjectAsync<T>(res);
PUT:
HttpResponseMessage response = _client.PutAsync("/put", new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json")).Result;
string res = await response.Content.ReadAsStringAsync();
return await JsonConvert.DeserializeObjectAsync<T>(res);
But DeleteAsync()
and ReadAsStringAsync()
always return me null
.
According to RFC you have to return body when returning status code 200 OK.
Your code never checks the message response for the StatusCode. Unlike WebClient, HttpClient does NOT throw when the StatusCode is not in the 2xx range.
I bet that if you check the HttpResponseMessage.StatusCode/HttpResonseMessage.ReasonPhrase values you will find that the server returned a code other than 200.
For example:
HttpClient _client = new HttpClient();
_client.BaseAddress = new Uri("http://httpbin.org/");
...
var response = await _client.DeleteAsync("/delete");
if (response.IsSuccessStatusCode)
{
var result=await response.Content.ReadAsStringAsync();
....
}
You can also call the EnsureSuccessStatusCode method to throw an exception if the response status is not a success code:
HttpClient _client = new HttpClient();
_client.BaseAddress = new Uri("http://httpbin.org/");
...
var response = await _client.DeleteAsync("/delete");
response.EnsureSuccessStatusCode();
var result=await response.Content.ReadAsStringAsync();
EDIT
By the way, running the following as is on .NET 4.5, returns a body:
var _client = new HttpClient();
_client.BaseAddress = new Uri("http://httpbin.org/");
var response = await _client.DeleteAsync("/delete");
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
Console.WriteLine(result);
}
Adding the Accept
header doesn't make any difference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With