Thursday, October 22, 2020

Copying Rest API command from Postman is not working in C# RestSharp? Here is the fix!

 As we all know if we click on the code button as on the image we can convert the Postman command to any sort of programming language.




Here are some languages we can convert the code.


But if you directly copy and past the code for C#-RestSharper then your code might not work.

There won't be any exceptions, But you will not get the response that you are expecting.

It always returns NULL response.


To make it work you have to do some adjustments.

Below is the original code and just comment the highlighted line which is  request.AddHeader("Connection", "keep-alive");



Actually to get this code work you need to add "using RestSharp" to your C# code.

Other thing is no need to add all these headers to get a valid output, You can simplify the above code by only having below lines.

request.AddHeader("Authorization",....);

request.AddHeader("Content-Type", "application/json");