I've been messing around with JSON for some time, just pushing it out as text and it hasn't hurt anybody (that I know of), but I'd like to start doing things properly.
I have seen so many purported "standards" for the JSON content type:
application/json
application/x-javascript
text/javascript
text/x-javascript
text/x-json
But which is correct, or best? I gather that there are security and browser support issues varying between them.
I know there's a similar question, What MIME type if JSON is being returned by a REST API?, but I'd like a slightly more targeted answer.
The right content type for JSON is
application/json
UNLESS you're using JSONP, also known as JSON with Padding, which is actually JavaScript and so the right content type would beapplication/javascript
.JSON (JavaScript Object Notation) and JSONP ("JSON with padding") formats seems to be very similar and therefor it might be very confusing which MIME type they should be using. Even though the formats seems to be very similar, there are some subtle differences between them.
So whenever in any doubts, I have very simple approach (which works perfectly find in most cases), namely, go and check corresponding RFC document.
JSON RFC 4627 (The application/json Media Type for JavaScript Object Notation (JSON)) is a specifications of JSON format. It says in section 6, that the MIME media type for JSON text is
JSONP JSONP ("JSON with padding") is handled different way than JSON, in a browser. JSONP is treated as a regular JavaScript script and therefor it should use
application/javascript,
the current official MIME type for JavaScript. In many cases, however,text/javascript
MIME type will work fine too.Note that
text/javascript
has been marked as obsolete by RFC 4329 (Scripting Media Types) document and it is recommended to useapplication/javascript
type instead. However, due to legacy reasons,text/javascript
is still widely used and it has cross-browser support (which is not always a case withapplication/javascript
MIME type, especially with older browsers).IANA has registered the official MIME Type for JSON as
application/json
.When asked about why not
text/json
, Crockford seems to have said JSON is not really JavaScript nor text and also IANA was more likely to hand outapplication/*
thantext/*
.More resources:
As many others have mentioned,
application/json
is the correct answer.But what haven't been explained yet is what the other options you proposed mean.
application/x-javascript
: Experimental MIME type for JavaScript beforeapplication/javascript
was made standard.text/javascript
: Now obsolete. You should useapplication/javascript
when using javascript.text/x-javascript
: Experimental MIME type for the above situation.text/x-json
: Experimental MIME type for JSON beforeapplication/json
got officially registered.All in all, whenever you have any doubts about content types, you should check this link
For JSON, I am using:
This is described in the IETF's JSON Data Interchange Format 7158 proposal, Section 1.2: Specifications of JSON.
If you get data from REST API in JSON so you have to use content-type