Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API Get requires Content-Type application/json;charset=UTF-8 - Issue with Http Client

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.

like image 658
Lee Ames Avatar asked Dec 28 '18 14:12

Lee Ames


People also ask

What is the meaning of content-type application json charset UTF-8?

Content-type: application/json; charset=utf-8 designates the content to be in JSON format, encoded in the UTF-8 character encoding. Designating the encoding is somewhat redundant for JSON, since the default (only?) encoding for JSON is UTF-8.

What is default encoding for application json?

The MIME media type for JSON text is application/json. The default encoding is UTF-8.

Is json always UTF-8?

The MIME media type for JSON text is application/json. JSON may be represented using UTF-8, UTF-16, or UTF-32.

What does content-type application json mean?

Content-Type. application/json. Indicates that the request body format is JSON. application/xml. Indicates that the request body format is XML.


2 Answers

Try to set the property:

 new MediaTypeHeaderValue("application/json")
        {
            CharSet = Encoding.UTF8.WebName
        }; 
like image 137
Iurie Marchitan Avatar answered Sep 18 '22 18:09

Iurie Marchitan


Try this one

HttpClient httpClient= new HttpClient();
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
like image 34
Roman Marusyk Avatar answered Sep 17 '22 18:09

Roman Marusyk