views:

63

answers:

4

1) Is it important to set the correct mime type for a web service response?

2) what is the correct mime type for a,
a) XML response?

b) JSON response?

application/xml 
text/xml
application/json
application/x-javascript
text/javascript
text/x-javascript
text/x-json
+2  A: 

If you don't set a MIME type, then the resource will not be interpreted correctly by the receiver.

Use text/xml and application/json respectively.

Jacob Relkin
Note that `text/xml` and `application/xml` have slightly different uses. `text/xml` indicates an XML format that is primarily human-readable/understandable. Either is fine though, just depends on the intended use of the XML output.
BoltClock
+1 for BoltClock. I wanted to write the same, but I seen his comment. Look at rfc3023 (see http://www.ietf.org/rfc/rfc3023.txt) and search for "readable by casual users". The link http://xml.silmaril.ie/developers/serversoftware/ has the main information from rfc3023 in the short form.
Oleg
+1  A: 

MIME type is important for the correctly interpretation by the receiver.

a) application/xml or text/xml

b) application/json

MIME Media Types

reinaldons
I think best just to stick with text/plain For both!
K001
If receiver application don't use the response mime type for anything, you can use any mime type. For best practices you must have to set correct mime type.
reinaldons
+1  A: 

1) Is it important to set the correct mime type for a web service response?

Yes. However, this is really dependent on how the recipient is expected or configured to process the content. A client which is built using out-of-band information about the content may choose to ignore the content type or a client can use the content-type metadata to route the representationt to an appropriate processing module based on the content type. Content-Type enables a client make sense of the content without having to peek into the actual content. Also, since you have marked your question as related to REST, it is important to understand the self-descriptive constraint of REST and what role the media types plays to acheive this constraint. If you are interested to learn more about self-descriptiveness, read section 5.2.1 of Roy's dissertation.

2) what is the correct mime type for a, a) XML response?

b) JSON response?

application/xml text/xml application/json application/x-javascript text/javascript text/x-javascript text/x-jsontext/x-json

All the above content types are generic and gives no more information to the client apart from saying if the content is JSON or XML. What you need is specific content-type for your application which not only tells your client about the format but also describes the semantics and how to process the content. As for the difference between application/xxx and text/xxx, RFC 3023 states the following:

If an XML document that is, the unprocessed, source XML document is readable by casual users, text/xml is preferable to application/xml. MIME user agents (and web user agents) that do not have explicit support for text/xml will treat it as text/plain, for example, by displaying the XML MIME entity as plain text. Application/xml is preferable when the XML MIME entity is unreadable by casual users. I have read in some discussions that text/xml may be deprecated in future but I am not sure of that.of that.

For JSON, the correct MIME type is application/json. See SO question.

Suresh Kumar
+1  A: 

1) Is it important to set the correct mime type for a web service response?

Absolutely yes. If you are doing a true REST API then documenting the different Media Types you return are a vital part of your API specification.

2) what is the correct mime type for a, a) XML response? b) JSON response?

For a true REST service, it depends on the details of your API and what you've defined as your content-types.

As an example (taken from this excellent article that is worth reading in full), a Bank may want to define a Content-Type for bank accounts of application/vnd.bank.org.account+xml. Note how the MIME type "ends with +xml, and as per RFC 3023, XML processors (including XMLHttpRequest) can handle such representations as though it is XML". The same bank might also use XML to represent a bank transfer, this time using a Content-Type of application/vnd.bank.org.transer+xml

Day