I cannot add Content-type as "application/x-www-form-urlencoded". There is throwing an error. Only for the this content-type. Thank You for the attention.
using (var httpClient = new HttpClient())
{
var request = new HttpRequestMessage
{
Method = new HttpMethod("POST"),
RequestUri = new Uri(path),
};
request.Headers.Add("Accept", "application/json");
request.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
HttpResponseMessage response1 = await httpClient.SendAsync(request);
var token = await response1.Content.ReadAsStringAsync();
}
It’s throwing error like that
"Misused header name, ‘Content-Type’. Make sure request headers are
used with HttpRequestMessage, response headers with
HttpResponseMessage, and content headers with HttpContent objects."
2
Answers
The content type is a header of the content, not of the request, which is why this is failing.
One way is that you can call
TryAddWithoutValidation
instead of add like below:The other way is that you can set the Content-Type when creating the request content itself, refer to this answer.
I’v used something more complicated but it works.