0

Whenever I am adding an HTTP body into my Http Request I am getting a 401 response code. The same request is working postman perfectly fine.

    var client = new RestClient("http://10.10.2.10:9000/haproxy_stats");
    client.Timeout = -1;
    var request = new RestRequest(Method.POST);
    request.AddHeader("Authorization", "Basic QURNSU46UGFzc3dvcmQtMTIzNA==");
    request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
    request.AddParameter("s", "server2");
    request.AddParameter("action", "ready");
    request.AddParameter("b", "#2");
    IRestResponse response = client.Execute(request);
    Console.WriteLine(response.Content);
  • I give this response almost daily. Use a sniffer like wireshark or fiddler. Compare the 1st request of working and non working. Then make c# request look like the working Postman. The default headers in c# are not the same as Postman. So you need to modify the c# application to change headers so they look like working Postman. – jdweng Apr 06 '20 at 21:53
  • Does this answer your question? [How to POST using HTTPclient content type = application/x-www-form-urlencoded](https://stackoverflow.com/questions/43158250/how-to-post-using-httpclient-content-type-application-x-www-form-urlencoded) – Joe Apr 07 '20 at 11:51
  • What does the request look like in Postman? – so cal cheesehead Apr 08 '20 at 19:49

1 Answers1

0

Get the code snippet from postman and copy the cookie header line .i hope it works for you

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 08 '22 at 23:53