Quantcast
Channel: How to set the Content-Type header for an HttpClient request? - Stack Overflow
Browsing all 25 articles
Browse latest View live

Answer by БранкоПејић for How to set the Content-Type header for an...

Since .NET 5 we can use static JsonContent.Create method.https://docs.microsoft.com/en-us/dotnet/api/system.net.http.json.jsoncontent.create?view=net-5.0It will set request Content-Type to...

View Article



Answer by Alberto Camino for How to set the Content-Type header for an...

By default Content-Type header is forbidden in GET methods, at least in -NET Framework implementation.I solve the problem using a custom Handler that inherits from the one used by HttpClient class. I...

View Article

Answer by CaMiX for How to set the Content-Type header for an HttpClient...

Setting the content type (my case I'm setting "application/vnd.api+json") like this worked for me:var requestBody = new StringContent("{"data": "1"}"), new...

View Article

Answer by bkwdesign for How to set the Content-Type header for an HttpClient...

So if you're trying to do a /$batch OData request like this Microsoft article demonstrates where you're supposed to have a Content-Type header like:Content-Type:...

View Article

Answer by purplecat for How to set the Content-Type header for an HttpClient...

For my scenario, a third-party API was creating the HttpRequestMessage, so I was not able to use the top-voted answers to resolve the issue. And I didn't like the idea of messing with reflection so the...

View Article


Answer by Muzoora Savior for How to set the Content-Type header for an...

Api returned"Unsupported Media Type","status":415Adding ContentType to the jsonstring did the magic and this is my script working 100% as of today using (var client = new HttpClient()) { var endpoint =...

View Article

Answer by Roland for How to set the Content-Type header for an HttpClient...

The trick is that you can just set all kinds of headers like:HttpRequestMessage request = new HttpRequestMessage();request.Headers.Add("Accept-Language", "en"); //works OKbut not any header. For...

View Article

Answer by zawar for How to set the Content-Type header for an HttpClient...

For those wanting to set the Content-Type to Json specifically, you can use the extension method PostAsJsonAsync.using System.Net.Http.Json; //this is needed for PostAsJsonAsync to work//....HttpClient...

View Article


Answer by Sérgio Damasceno for How to set the Content-Type header for an...

I got the answer with RestSharp:private async Task<string> GetAccessTokenAsync(){ var client = new RestClient(_baseURL); var request = new RestRequest("auth/v1/login", Method.POST,...

View Article


Answer by yoel halb for How to set the Content-Type header for an HttpClient...

For anyone that needs the content-type header for Get etc., while in an older .NET version it is possible to use the answer of @erdomke at https://stackoverflow.com/a/41231353, this unfortunately no...

View Article

Answer by Felipe Augusto for How to set the Content-Type header for an...

try to use HttpClientFactoryservices.AddSingleton<WebRequestXXX>() .AddHttpClient<WebRequestXXX>("ClientX", config => { config.BaseAddress = new...

View Article

Answer by Hassan Faghihi for How to set the Content-Type header for an...

For those who troubled with charsetI had very special case that the service provider didn't accept charset, and they refuse to change the substructure to allow it...Unfortunately HttpClient was setting...

View Article

Answer by user890332 for How to set the Content-Type header for an HttpClient...

You need to do it like this: HttpContent httpContent = new StringContent(@"{ the json string }"); httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");...

View Article


Answer by Kumaran for How to set the Content-Type header for an HttpClient...

You can use this it will be work!HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Get,"URL");msg.Content = new StringContent(string.Empty, Encoding.UTF8,...

View Article

Answer by leocrimson for How to set the Content-Type header for an HttpClient...

I find it most simple and easy to understand in the following way:async Task SendPostRequest(){ HttpClient client = new HttpClient(); var requestContent = new StringContent(<content>);...

View Article


Answer by Ziba Leah for How to set the Content-Type header for an HttpClient...

Ok, it's not HTTPClient but if u can use it, WebClient is quite easy:using (var client = new System.Net.WebClient()) { client.Headers.Add("Accept", "application/json");...

View Article

Answer by art24war for How to set the Content-Type header for an HttpClient...

var content = new JsonContent();content.Headers.ContentType = new MediaTypeHeaderValue("application/json");content.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("charset",...

View Article


Answer by Jay for How to set the Content-Type header for an HttpClient request?

Some extra information about .NET Core (after reading erdomke's post about setting a private field to supply the content-type on a request that doesn't have content):After debugging my code, I can't...

View Article

Answer by erdomke for How to set the Content-Type header for an HttpClient...

.Net tries to force you to obey certain standards, namely that the Content-Type header can only be specified on requests that have content (e.g. POST, PUT, etc.). Therefore, as others have indicated,...

View Article

Answer by SharpCoder for How to set the Content-Type header for an HttpClient...

try to use TryAddWithoutValidation var client = new HttpClient(); client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");

View Article
Browsing all 25 articles
Browse latest View live




Latest Images