Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
Have you tried to use Fiddler to see what's going on on the wire? – Paulo Morgado Jul 14 at 19:43
1  
Shouldn't make a difference on the result, but any reason why you block (call .Result) on DeleteAsync instead of awaiting it? – cremor Jul 15 at 5:15
@cremor Rookie mistake. – Gaui Jul 15 at 13:31

1 Answer

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

share|improve this answer
Changed my code to use response.EnsureSuccessStatusCode(); - but that didn't change anything. The server respondes with 200 OK. But I always get null. pastebin.com/dKb8BfBD – Gaui Jul 15 at 13:35
Why do you thing this is wrong? The server may not have sent a body. You should check the server's API to see whether it returns anything on a DELETE. Besides, not all REST APIs respect the RFCs – Panagiotis Kanavos Jul 15 at 13:43
I'm guessing ASP.NET Web API doesn't respect it. Because the method I'm using to deliver the data is exactly the same as POST and PUT. Weird. – Gaui Jul 15 at 15:13
ASP.NET Web API returns what you tell it to return. If you don't return anything for the DELETE verb, there is nothing to return. In other words, check your code – Panagiotis Kanavos Jul 15 at 17:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.