I'm trying to set the Content-Type
header of an HttpClient
object as required by an API I am calling.
I tried setting the Content-Type
like below:
using (var httpClient = new HttpClient()){ httpClient.BaseAddress = new Uri("http://example.com/"); httpClient.DefaultRequestHeaders.Add("Accept", "application/json"); httpClient.DefaultRequestHeaders.Add("Content-Type", "application/json"); // ...}
It allows me to add the Accept
header but when I try to add Content-Type
it throws the following exception:
Misused header name. Make sure request headers are used with
HttpRequestMessage
, response headers withHttpResponseMessage
, and content headers withHttpContent
objects.
How can I set the Content-Type
header in a HttpClient
request?