rest - What is the correct JSON content type?

ID : 6

viewed : 183

Tags : jsonresthttp-headersmime-typescontent-typejson

Top 5 Answer for rest - What is the correct JSON content type?

vote vote

98

For JSON text:

application/json 

The MIME media type for JSON text is application/json. The default encoding is UTF-8. (Source: RFC 4627)

For JSONP (runnable JavaScript) with callback:

application/javascript 

Here are some blog posts that were mentioned in the relevant comments:

vote vote

87

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 out application/* than text/*.

More resources:

vote vote

78

For JSON:

Content-Type: application/json 

For JSON-P:

Content-Type: application/javascript 
vote vote

67

Of course, the correct MIME media type for JSON is application/json, but it's necessary to realize what type of data is expected in your application.

For example, I use Ext GWT and the server response must go as text/html but contains JSON data.

Client side, Ext GWT form listener

uploadForm.getForm().addListener(new FormListenerAdapter() {     @Override     public void onActionFailed(Form form, int httpStatus, String responseText)      {         MessageBox.alert("Error");     }      @Override     public void onActionComplete(Form form, int httpStatus, String responseText)      {         MessageBox.alert("Success");     } }); 

In case of using application/json response type, the browser suggests me to save the file.

Server side source code snippet using Spring MVC

return new AbstractUrlBasedView()  {     @SuppressWarnings("unchecked")     @Override     protected void renderMergedOutputModel(Map model, HttpServletRequest request,                                            HttpServletResponse response) throws Exception      {         response.setContentType("text/html");         response.getWriter().write(json);     } }; 
vote vote

56

JSON:

Response is dynamically generated data, according to the query parameters passed in the URL.

Example:

{ "Name": "Foo", "Id": 1234, "Rank": 7 } 

Content-Type: application/json


JSON-P:

JSON with padding. Response is JSON data, with a function call wrapped around it.

Example:

functionCall({"Name": "Foo", "Id": 1234, "Rank": 7}); 

Content-Type: application/javascript

Top 3 video Explaining rest - What is the correct JSON content type?

Related QUESTION?