Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# httpclient PostAsJson sending GET request instead of POST

I am using HttpClient to make a post request. I get back 405 method not allowed. When capturing a trace in fiddler, it goes out as GET instead of POST!

using (var client = new HttpClient())
            {
                var url = AppSettingsUtil.GetString("url");
                var response = client.PostAsJsonAsync(url, transaction).Result;
            }

I am aware of the async/await issues. This is a simplified sample to show the issue.

Is there some sort of web.config or machine.config setting that could be affecting this? Other requests (sent through RestSharp) send Posts correctly

Here is what fiddler captures. Rerunning the trace in fiddler also returns the 405 (as expected). Manually switching it to POST and running works from fiddler.

Also, perhaps because the method was switched to GET, there is no body captured in fiddler, I had to manually paste in the JSON

GET /*URL*/ HTTP/1.1
Content-Type: application/json; charset=utf-8
Host: /*host*/
Connection: Keep-Alive
like image 349
Jason Coyne Avatar asked Jan 29 '16 22:01

Jason Coyne


1 Answers

The problem appears to be that someone changed the URL without telling us, and they put a redirect in place. HttpClient is responding to the redirect, but ends up actually sending the request to the final destination as a Get.

This seems like a bug in HttpClient to me, that it should either send the ultimate request as a Post, or throw an exception saying it can't do what I asked it to.

See Forwarding a response from another server using JAX-RS

like image 78
Jason Coyne Avatar answered Oct 18 '22 22:10

Jason Coyne