Kwang Yul Seo
Kwang Yul Seo

Reputation: 791

Default encoding of HTTP POST request with JSON body

What's the default encoding of HTTP POST request when the content-type is "application/json" with no explicit charset given"?

It seems two specs are in conflicts:

Upvotes: 10

Views: 17562

Answers (3)

Remy Lebeau
Remy Lebeau

Reputation: 598134

The application/json media type is formally defined in RFC 7158 The JavaScript Object Notation (JSON) Data Interchange Format (which obsoletes RFC 4627), and is registered with IANA has having NO required or optional parameters (thus, charset is not defined for application/json).

Section 8.1 Character Encoding says:

JSON text SHALL be encoded in UTF-8, UTF-16, or UTF-32. The default encoding is UTF-8, and JSON texts that are encoded in UTF-8 are interoperable in the sense that they will be read successfully by the maximum number of implementations; there are many implementations that cannot successfully read texts in other encodings (such as UTF-16 and UTF-32).

Implementations MUST NOT add a byte order mark to the beginning of a JSON text. In the interests of interoperability, implementations that parse JSON texts MAY ignore the presence of a byte order mark rather than treating it as an error.

application/... media types are typically defined as binary formats. It is very easy for a JSON parser to differentiate between UTF-8, UTF-16, and UTF-32 just by looking at the first few bytes, so there is no need for a BOM (which is not allowed, as noted above) or an explicit charset (which is not defined).

Upvotes: 7

Julian Reschke
Julian Reschke

Reputation: 42075

You are looking at RFC 2616, which has been obsoleted by RFC 7231. The text from RFC 2616 is gone in RFC 7231. In any case, the clause only applies to the text/... media types, not application/... media types.

Upvotes: 1

Jerome Anthony
Jerome Anthony

Reputation: 8031

Here is the algorithm form XMLHttpRequest from W3C

The JSON response entity body is either a JavaScript value representing the response entity body. If the JSON response entity body is null, set it to the return value of the following algorithm:

1. Let JSON text be the result of running utf-8 decode on byte stream response entity body.

2. Return the result of invoking the initial value of the parse property of the JSON object defined in JavaScript, with JSON text as

its only argument, or null if that function throws an exception.

http://www.w3.org/TR/XMLHttpRequest/#json-response-entity-body

The server should set it as UTF-8 by default.

Upvotes: 1

Related Questions