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.
In JSP, you can use this in page directive:
The correct MIME media type for JSON is
application/json
. JSP will use it for sending a response to the client.The Content-Type header should be set to 'application/json' when posting. Server listening for the request should include "Accept=application/json". In Spring MVC you can do it like this:
Add headers to the response:
In Spring you have a defined type:
MediaType.APPLICATION_JSON_VALUE
which is equivalent to application/json.I use this code to put data in JSON on Google Cloud Storage (GCS) which is set publically viewable:
To get back the data is straight forward:
The IANA registration for
application/json
saysYou'll notice that IANA.org doesn't list any of these other media types, in fact even
application/javascript
is now obsolete. Soapplication/json
is really the only possible correct answer.Browser support is another thing.
The most widely supported non-standard media types are
text/json
ortext/javascript
. But some big names even usetext/plain
.Even more strange is the Content-Type header sent by Flickr, who returns JSON as
text/xml
. Google usestext/javascript
for some of it's ajax apis.Examples:
Output:
Content-Type: text/javascript
Output:
Content-Type: text/xml
Only when using
application/json
as the MIME type I have the following (as of November 2011 with the most recent versions of Chrome, Firefox with Firebug):