Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can charset parameter be used with application/json content type in http/1.1?

For example, is it valid ajax request:

$.ajax({
    type: "POST",
    url: "SomePage.aspx/GetSomeObjects",
    contentType: "application/json; charset=utf-8",
    ...
});

It is used as an example sometimes, or software can break without explicit charset.

The rfc 4627 for application/json media type says that it doesn't accept any parameters in section 6:

The MIME media type for JSON text is application/json.

Type name: application

Subtype name: json

Required parameters: n/a

Optional parameters: n/a

It can be interpreted that charset shouldn't be used with application/json.

And section 3 suggests that it is not necessary to specify charset:

JSON text SHALL be encoded in Unicode.  The default encoding is
UTF-8.

Since the first two characters of a JSON text will always be ASCII
characters [RFC0020], it is possible to determine whether an octet
stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking
at the pattern of nulls in the first four octets.

        00 00 00 xx  UTF-32BE
        00 xx 00 xx  UTF-16BE
        xx 00 00 00  UTF-32LE
        xx 00 xx 00  UTF-16LE
        xx xx xx xx  UTF-8

because UTF-8,16,32 encodings can be infered from the content. Why does it say that UTF-8 is default? The way to choose other character encoding is not specified in the rfc and the encoding can be found deterministically anyway. Or are there other (not UTF-8,16,32) character encodings that support Unicode?

Some argue that charset can be used:

I disagree with your assessment that it must be dropped. RFC 2046 states that "other media types than subtypes of "text" might choose to employ the charset parameter as defined here," which indicates that there is no restriction on the presence of the charset parameter on application types. Additionally, RFC 2045 states that "MIME implementations must ignore any parameters whose names they do not recognize." So, it is not reasonable to assume that there is any harm being done by its presence.

May rfc-compliant software generate content type application/json with a charset parameter? Should rfc-compliant software accept such requests?

like image 792
jfs Avatar asked Oct 27 '12 00:10

jfs


People also ask

What charset does JSON use?

6 Character Sets and Character Encoding for JSON Data Textual JSON data always uses the Unicode character set.

What is default encoding for application JSON?

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

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.

Does JSON use UTF-8?

The JSON spec requires UTF-8 support by decoders. As a result, all JSON decoders can handle UTF-8 just as well as they can handle the numeric escape sequences. This is also the case for Javascript interpreters, which means JSONP will handle the UTF-8 encoded JSON as well.


1 Answers

application/json doesn't define a charset parameter, so it is incorrect to include one. What RFC 2046 says is that application types in general could have a charset parameter, such as application/xml. But JSON does not.

like image 177
Julian Reschke Avatar answered Sep 22 '22 17:09

Julian Reschke