What is the correct JSON content type?

2018-12-30 23:44发布

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.

30条回答
何处买醉
2楼-- · 2018-12-31 00:11

In JSP, you can use this in page directive:

<%@ page language="java" contentType="application/json; charset=UTF-8"
    pageEncoding="UTF-8"%>

The correct MIME media type for JSON is application/json. JSP will use it for sending a response to the client.

查看更多
忆尘夕之涩
3楼-- · 2018-12-31 00:11

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:

@RequestMapping(value="location", method = RequestMethod.POST, headers = "Accept=application/json")

Add headers to the response:

HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
查看更多
谁念西风独自凉
4楼-- · 2018-12-31 00:11

In Spring you have a defined type: MediaType.APPLICATION_JSON_VALUE which is equivalent to application/json.

查看更多
不流泪的眼
5楼-- · 2018-12-31 00:12

The application/json works great in PHP to store an array or object data.

I use this code to put data in JSON on Google Cloud Storage (GCS) which is set publically viewable:

$context = stream_context_create([
    'gs' => [
        'acl'=>'public-read', 
        'Content-Type' => 'application/json',
    ]
]);

file_put_contents(
    "gs://BUCKETNAME/FILENAME.json", 
    json_encode((object) $array), 
    false, 
    $context
);

To get back the data is straight forward:

$data = json_decode(file_get_contents("gs://BUCKETNAME/FILENAME.json"));
查看更多
旧人旧事旧时光
6楼-- · 2018-12-31 00:13

The IANA registration for application/json says

Applications that use this media type: JSON has been used to exchange data between applications written in all of these programming languages: ActionScript, C, C#, Clojure, ColdFusion, Common Lisp, E, Erlang, Go, Java, JavaScript, Lua, Objective CAML, Perl, PHP, Python, Rebol, Ruby, Scala, and Scheme.

You'll notice that IANA.org doesn't list any of these other media types, in fact even application/javascript is now obsolete. So application/json is really the only possible correct answer.

Browser support is another thing.

The most widely supported non-standard media types are text/json or text/javascript. But some big names even use text/plain.

Even more strange is the Content-Type header sent by Flickr, who returns JSON as text/xml. Google uses text/javascript for some of it's ajax apis.

Examples:

curl -I "https://ajax.googleapis.com/ajax/services/search/video?v=1.0&q=jsonexample"

Output: Content-Type: text/javascript

curl -I "https://www.flickr.com/services/rest/?method=flickr.test.echo&format=json&api_key=f82254c1491d894f1204d8408f645a93"

Output: Content-Type: text/xml

查看更多
一个人的天荒地老
7楼-- · 2018-12-31 00:15

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):

  • No more warnings from Chrome when the JSON is loaded from the server.
  • Firebug will add a tab to the response showing you the JSON data formatted. If the MIME type is different, it will just show up as 'Response content'.
查看更多
登录 后发表回答