Quantcast
Channel: How to set the Content-Type header for an HttpClient request? - Stack Overflow
Viewing all articles
Browse latest Browse all 25

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

$
0
0

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 withHttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.

How can I set the Content-Type header in a HttpClient request?


Viewing all articles
Browse latest Browse all 25

Latest Images

Trending Articles





Latest Images