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[@] Your question and answer will appear on and help other developers.

Related Questions & Answers