HttpClient getting different results than Swagger

Viewed 43

I have a ASP.NET Web API that returns a bad request response like below.

public async Task<System.Web.Http.IHttpActionResult> Delete(int id)
{
    return BadRequest("test");
}

It works well when I test with Swagger.

curl -X DELETE --header 'Accept: application/json' 'http://localhost:12345/api/Items/123'

I get a 400 error code with a message as below.

{
  "Message": "test"
}

But when I test with a HttpClient object like below, I only get back a 400 error "Bad Request" but no message like above.

[HttpPost]
public JsonResult Delete(int id)
{
    using(var hc = new HttpClient {BaseAddress = new Uri(http://localhost:12345/)}){
        hc.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        return Json(hc.DeleteAsync("api/Items/123").Result, JsonRequestBehavior.DenyGet);
    }
}
0 Answers
Related