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