Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

Just for the record: specifying a charset for application/json is indeed meaningless. I'd report that as a bug. Julian Reschke 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. Lee Ames Dec 28, 2018 at 16:13 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. Lee Ames Dec 28, 2018 at 15:13

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);
                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.
– Epicycle
                Jun 27, 2022 at 15:50

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();               

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);
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;
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.