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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer by archgl for How to set the Content-Type header for an HttpClient...
req.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
View ArticleAnswer 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 ArticleAnswer 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 ArticleHow 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
More Pages to Explore .....