What MIME type if JSON is being returned by a REST API?
My REST API returns JSON.
I'm currently returning text/plain as the MIME type, but it feels funny. Should I be returning application/x-javascript
or some other type?
The second question is with regard to the HTTP status code for error conditions. If my REST API is returning an error state, I am returning as JSON
{ result: "fail", errorcode: 1024, errormesg: "That sucked. Try again!" }
Should the HTTP status code remain at 200 OK
?
The JSON spec suggests application/json
, and that seems to be supported by the IETF and IANA registry.
On the second question, I think if the message handling fails in some way you should return a structured and standard error response as a JSON message; only if there is a failure to deliver the message to the backend handler for some reason should you consider an HTTP error code.
Update 2014-06-27 : The days where clients (browsers) only worked with a 200 response are long past and the prevailing advice for RESTful APIs is to use HTTP response codes appropriate for the response, 2xx for successful responses (eg 201 Created for PUT; 204 No Content for DELETE) and 4xx and 5xx for all error conditions, including those from the API itself.
The MIME type of JSON is
application/json
http://www.ietf.org/rfc/rfc4627.txt
http://www.iana.org/assignments/media-types/application/
More specifically here:
http://www.ietf.org/rfc/rfc4627.txt
我更愿意回复HTTP错误状态和特定于应用程序的有效内容。
链接地址: http://www.djcxy.com/p/118.html上一篇: 在jQuery中序列化为JSON