Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine what is the issue with a 500 error on a HttpClient.PostAsJsonAsync call?

I am making a call to a Web API that I wrote. I am working through the bugs on both sides and am getting a 500 error. I want to see that is in that error message to see what might be the problem. How do I find that?

using (var client = new HttpClient())
{
    var fooURL = Url.RouteUrl("PayrollApi", new { httproute = string.Empty, controller = "LeaveRequest" }, Request.Url.Scheme);
    var repsonse = client.PostAsJsonAsync(fooURL, leaveRequest).Result;
}

I don't see where that text might be stored so I can figure out what other bugs need to be fixed. How do I get that text?

Update: something I didn't clarify. I put a breakpoint on the WebAPI I am calling and the break point is never hit. However, when I call it from Poster, I hit the break point. The URL is correct.

public HttpResponseMessage Post([FromBody]LeaveRequest value)
{
    if (ModelState.IsValid)
    {
        // code here
    }
}
like image 779
Mike Wills Avatar asked May 06 '13 04:05

Mike Wills


1 Answers

I was able to make a change to get the error message:

var repsonse = client.PostAsJsonAsync(fooURL, leaveRequest).Result.Content.ReadAsStringAsync();

instead of

var repsonse = client.PostAsJsonAsync(fooURL, leaveRequest).Result;

I was then able to see my error and fix it.

like image 125
Mike Wills Avatar answered Oct 11 '22 15:10

Mike Wills