Content Content Request Header

I am making an ajax call to the rest API and set the following HTTP request header.

Content-Type application/json; charset=UTF-8 

There are some Japanese / Chinese characters in my writing body.

Now, what is my question, do I need the encoding of the body of the mail request with UTF-8 encoding, or does the browser take care of the encoding?

+4
source share
1 answer

When your Content-Type header declares UTF-8 encoding, you must send UTF-8 encoded content.

Although browsers sometimes β€œguess” or β€œfix” the encoding, you should never rely on this, as it is a very fragile logic that often does not work properly.

If your Chinese / Japanese content was in a different encoding (e.g. Shift-JIS), you will have to convert the text to a library, such as iconv .

Alternatively, you can declare this other encoding in the header, but note that you can only use one encoding for the entire response body. The best solution is to convert everything to UTF-8.

+4
source

Source: https://habr.com/ru/post/1382666/


All Articles