How do you set the Content-Type header for an HttpClient request?

Posted by Rohit Kumar | Updated on

If you want to set the Content-Type header for an HttpClient request in C#? Use the following C# example code.

The content type is a header of the content, not of the request, which is why this is failing. AddWithoutValidation as suggested by Robert Levy may work, but you can also set the content type when creating the request content itself (note that the code snippet adds “application/json” in two places-for Accept and Content-Type headers):


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers