0

How can you set the default content type for a get request with HttpClient. I read .NET does not allow it as written here which is incredibly frustrating as some APIs require it. I am getting the

Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.

error and the replies in the link don't answer the problem.

In fact I was trying to use HttpClient over HttpWebRequest for its suppoused "ease of use" but it is totally the opposite.

With HttpWebRequest I had already achieved what I wanted.

HttpWebRequest request=(httpWebRequest)WebRequest.create(uri);
request.ContentType="application/json";
request.Headers.Add("customheader","blablabla");
var response=(HttpWebResponse) await request.GetResponseAsync();

Very simple! I know it does not set the header by default but at least it let me set the content type for a request.

Should I stop using HttpClient??

(I repeat the "duplicate solution" that actully >I< indicated do not provide a solution:

Addwithoutvalidation does not exist. TryAddWithoutValidation do exist but does not set the header (Almost all the "solutions" suggest this). The other solution in the "duplicate" sets the content type for Post but not Get. )

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
  • 1
    Possible duplicate of [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) – AndreasHassing Jun 12 '18 at 08:55
  • 1
    I already wrote that the replies in the link do not answer the problem. – KansaiRobot Jun 12 '18 at 08:58
  • since you mention that it's a GET request, why do you need to set the content type? Normally you don't send a body with a GET request. – ADyson Jun 12 '18 at 09:00
  • Because it is a requirement of the Web Api I have to use – KansaiRobot Jun 12 '18 at 09:00
  • 1
    "the replies in the link do not answer the problem"...it would probably help people to understand _why_ that doesn't work for you – ADyson Jun 12 '18 at 09:00
  • 1
    "it is a requirement of the Web Api I have to use"...if the API isn't expecting a body in the request, then I doubt that it will care what the content type is...it isn't trying to read any content in that case. Unless this is an unusual situation where a GET request is sent with a request body? – ADyson Jun 12 '18 at 09:02
  • the Get does not request a body. However it absolutely requires the content type. It is a requirement of a API that I did not built, I just have to use it (I read there are other APIs like this too) – KansaiRobot Jun 12 '18 at 09:04
  • so you get some error from the API if a content-type header is not sent in the request? What response do you get exactly in that situation? Sounds like the API is badly specified. – ADyson Jun 12 '18 at 09:08
  • Yes. I have already confirm that I get the error specified in their specifications – KansaiRobot Jun 12 '18 at 09:09
  • and, as I asked, what error is that? And also please explain why the solution in the duplicate doesn't work for you? Doesn't it set the header? Lots of people seem to think the accepted answer is a good one. – ADyson Jun 12 '18 at 09:12
  • the error is #415 Unsopported Media Type (just as they wrote in their specs) so at least I know it is working. Anyway, using `HttpWebRequest` I have already achieved my goal, I just though using HttpClient would have being a good idea – KansaiRobot Jun 12 '18 at 09:18
  • The solution accepted in the other link is to avoid the error message by setting the headers for anything other than get. The other solutions simply are not even compilable – KansaiRobot Jun 12 '18 at 09:20
  • Have you seen this then @KansaiRobot https://stackoverflow.com/a/47902348/618441? – AndreasHassing Jun 12 '18 at 10:00

0 Answers0