REST - web service response - mime type?
1) Is it important to se开发者_运维百科t 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
- 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 documentation of the different Media Types you return is a vital part of your API specification.
- 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
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.
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.
MIME type is important for the correctly interpretation by the receiver.
a) application/xml or text/xml
b) application/json
MIME Media Types
1) Is it important to set the correct mime type for a web service response?
Yes, because of the uniform interface / self descriptive message constraint. The messages have to contain all information necessary to process them.
2) what is the correct mime type for a,
It depends on what the client asks. You have to check the accept header. We usually use application/xml
by services and text/xml
by serving static files. But it does not really matter.
If you send hyperlinks to you can use a vendor specific MIME, or you can use some linked data solution, like JSON-LD.
精彩评论