Quantcast
Channel: CameTooFar
Viewing all articles
Browse latest Browse all 45

Fix: Error on posting data using HttpClient

$
0
0

 Last week, I’s spending some time exploring OneDrive Rest API. While trying to send a POST request for receiving access token, I got a error as - “{"error":"invalid_request","error_description":"The provided request must have content-type 'application/x-www-form-urlencoded'."}”. 

I modified my code further. This time, I got another exception – “Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.” Finally, I figured out the correct way to set the content-type header while sending a POST request using HTTPClient. And, here it is.

HttpClient client = new HttpClient();
StringContent content = new StringContent(BodyContent, Encoding.UTF8, "application/x-www-form-urlencoded");
var responseMessage = await client.PostAsync(Uri, content);

Phew! Hope this helps.

Thanks.


Viewing all articles
Browse latest Browse all 45

Trending Articles