12

I am working with an API service that requires Content-Type to be set to application/json;charset=UTF-8.

If I make a request without the charset=UTF-8 I get a 406 - Not Acceptable.

I can make a call through Postman setting the Content-Type as required, but if I use my .Net Http Client I get the error:

System.FormatException: 'The format of value 'application/json;charset=UTF-8' is invalid.'

Is there anyway I can work around this validation and force the Http Client to accept the value?

UPDATE:

Here is my latest attempt,it still throws the error.

Body.Headers.ContentType = new MediaTypeHeaderValue("application/json;charset=UTF-8");

UPDATE: Content-Type is indeed an invalid header. The API Developers removed it at our request.

7
  • Could you please show us the code where you set Content-Type for httpclient? Commented Dec 28, 2018 at 14:53
  • 1
    @RomanMarusyk No problem, I just added it above. Thanks! Commented Dec 28, 2018 at 14:57
  • It is not enough. Please add more code how do you make a request Commented Dec 28, 2018 at 15:22
  • 1
    Just for the record: specifying a charset for application/json is indeed meaningless. I'd report that as a bug. Commented Dec 28, 2018 at 15:51
  • @JulianReschke, that was what I was suspecting. Do you know where I can find documentation to share with the API developers to convince them remove the requirement? I assume it is violating a standard somewhere. Commented Dec 28, 2018 at 16:13

6 Answers 6

17

Try to set the property:

 new MediaTypeHeaderValue("application/json") { CharSet = Encoding.UTF8.WebName }; 
Sign up to request clarification or add additional context in comments.

Comments

6

Try this one

HttpClient httpClient= new HttpClient(); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8"); 

1 Comment

I did try this one, it returns false, indicating that it did not add the header. Also when I examine the client object I can see that it was not added to the list of headers. But thanks for the suggestion.
1

Not sure if still relevant, but I recently ran into this same issue and was able to solve by setting the header in the following way:

string str = $"application/vnd.fmsstandard.com.Vehicles.v2.1+json; charset=UTF-8"; client.DefaultRequestHeaders.Add("Accept", str); 

1 Comment

Probably better do a client.DefaultRequestHeaders.Accept.Clear() before but other than that this should be the accepted answer. Edit: Nevermind op is talking about the content-type not the accept header.
0

Try adding double quotes around UTF-8, like this:

Body.Headers.ContentType = new MediaTypeHeaderValue("application/json;charset=\"UTF-8\""); 

EDIT:

Ok, try something like this. It's working for me locally with a WebApi I already had handy. Notice there is a header specification for what content-type will be ACCEPTED, and then there is a header for what content-type will be SENT with the request. For this example, both of them are JSON:

public static async Task<string> HttpClient(string url) { using(HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(url); client.DefaultRequestHeaders .Accept .Add(new MediaTypeWithQualityHeaderValue("application/json")); // ACCEPT header HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, ""); request.Content = new StringContent("{\"id\" : 1}", Encoding.UTF8, "application/json"); // REQUEST header HttpResponseMessage response = await client.SendAsync(request); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStringAsync(); } } 

1 Comment

I got the same validation error as before. But I do appreciate the suggestion!
0

I only added the authentication header to it and it worked for me. AuthToken is either a string variable or the token itself. I left out the content type header and it just works. Below is the code; Response is a string that has to be serialized to a Jobject.

{ String Response = null; HttpClient client = new HttpClient(CertByPass()); client.Timeout = TimeSpan.FromMinutes(5); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(AuthToken); Response = await client.GetStringAsync(url); } 

Comments

-1

Try creating a client helper class like:

HttpClient client = new HttpClient(); client.BaseAddress = new Uri(whatever your url); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); return client; 

1 Comment

The OP says that "a request without the charset=UTF-8 I get a 406 - Not Acceptable."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.