0

I am trying to add Content-Type header to HttpClient GET request, here my code:

HttpClient client=new ....
bool added = client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded");
var response = await client.GetAsync(...

but the added variable is false, i.e it failed to add the header.

How can I add this header?

NOTE:

This post deals with POST request, I asked about GET

spez
  • 409
  • 8
  • 21
  • Does this answer your question? [How do you set the Content-Type header for an HttpClient request?](https://stackoverflow.com/questions/10679214/how-do-you-set-the-content-type-header-for-an-httpclient-request) – dovid Nov 24 '20 at 13:58
  • @dovid the link deals with POST, I asked about GET – spez Nov 24 '20 at 13:58
  • 1
    You don't. A `GET` request cannot have a body and thus does not have content. `x-www-form-urlencoded` applies *only* to `POST`; the equivalent for `GET` is to simply add the parameters to the URL. – Jeroen Mostert Nov 24 '20 at 14:00
  • @JeroenMostert I made some GET request with chrome browser and one of the request headers is Content-Type, it can be seen in DevTools under network tab – spez Nov 24 '20 at 14:04
  • I suppose adding the header is technically not illegal, and even adding a payload is not illegal (the standard just says it has no defined semantics). For a server to *require* the header to process the request correctly would still be in very poor form -- even more so if it actually required a body to go with it. Double check that adding the header is actually needed to make things work. – Jeroen Mostert Nov 24 '20 at 14:09

2 Answers2

0

If you look at the Http/1.1 specification:

A sender that generates a message containing a payload body SHOULD generate a Content-Type header field in that message unless the intended media type of the enclosed representation is unknown to the sender. If a Content-Type header field is not present, the recipient MAY either assume a media type of "application/octet-stream" ([RFC2046], Section 4.5.1) or examine the data to determine its type.

Check also the MDN on get requests

The HTTP GET method requests a representation of the specified resource. Requests using GET should only retrieve data.

Sending body/payload in a GET request may cause some existing implementations to reject the request — while not prohibited by the specification, the semantics are undefined. It is better to just avoid sending payloads in GET requests.

Effectively, that means that wether you send or not the header, it's going to be ignored and/or rejected.

When setting the content type, it's better to set it from the content itself: How do you set the Content-Type header for an HttpClient request?

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • but `client.GetAsync(...` happens only after trying adding the header, i.e it doesn't know yet that I'm trying to do a `GET` request – spez Nov 26 '20 at 13:29
0

Im currently working on a project, where I call an api using a POST request. This might help in your case. Its how its done in an official Microsoft Documentation.

using (var content = new ByteArrayContent(byteData))
{
// This example uses the "application/octet-stream" content type.
// The other content types you can use are "application/json"
// and "multipart/form-data".
content.Headers.ContentType = new mediaTypeHeaderValue("application/octet-stream");
response = await client.PostAsync(uriBase, content);
}
Jonas Heinze
  • 45
  • 1
  • 1
  • 8