Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

like image 294
mynameiscoffey Avatar asked May 21 '12 03:05

mynameiscoffey


People also ask

How do you pass headers for HttpClient?

In versions pre 4.3 of HttpClient, we can set any custom header on a request with a simple setHeader call on the request: HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(SAMPLE_URL); request. setHeader(HttpHeaders. CONTENT_TYPE, "application/json"); client.

What is the Content-Type in header of HTTP?

The Content-Type representation header is used to indicate the original media type of the resource (prior to any content encoding applied for sending). In responses, a Content-Type header provides the client with the actual content type of the returned content.

Do I need a Content-Type header for HTTP GET requests?

Nope, Content-Type is not a required field. It's not mandatory per the HTTP 1.1 specification. Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body.


2 Answers

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):

HttpClient client = new HttpClient(); client.BaseAddress = new Uri("http://example.com/"); client.DefaultRequestHeaders       .Accept       .Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header  HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "relativeAddress"); request.Content = new StringContent("{\"name\":\"John Doe\",\"age\":33}",                                     Encoding.UTF8,                                      "application/json");//CONTENT-TYPE header  client.SendAsync(request)       .ContinueWith(responseTask =>       {           Console.WriteLine("Response: {0}", responseTask.Result);       }); 
like image 136
carlosfigueira Avatar answered Nov 11 '22 05:11

carlosfigueira


For those who didn't see Johns comment to carlos solution ...

req.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); 
like image 21
archgl Avatar answered Nov 11 '22 06:11

archgl