Quantcast
Channel: How to set the Content-Type header for an HttpClient request? - Stack Overflow
Browsing latest articles
Browse All 25 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

Answer by Todd Menier for How to set the Content-Type header for an...

If you don't mind a small library dependency, Flurl.Http [disclosure: I'm the author] makes this uber-simple. Its PostJsonAsync method takes care of both serializing the content and setting the...

View Article


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

req.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

View Article


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

The content type is a header of the content, not of the request, which is why this is failing. AddWithoutValidation as suggested by Robert Levy may work, but you can also set the content type when...

View Article

Answer by Robert Levy for How to set the Content-Type header for an...

Call AddWithoutValidation instead of Add (see this MSDN link).Alternatively, I'm guessing the API you are using really only requires this for POST or PUT requests (not ordinary GET requests). In that...

View Article

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

I'm trying to set the Content-Type header of an HttpClient object as required by an API I am calling.I tried setting the Content-Type like below:using (var httpClient = new HttpClient()){...

View Article

Browsing latest articles
Browse All 25 View Live




Latest Images