tags:

views:

1654

answers:

2

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 types ?

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 remained at 200 OK ?

+8  A: 

The JSON spec suggests application/json.

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.

Software Monkey
Thanks for the link to the JSON spec. I found another stackoverflow question that points to another MIME type "text/x-json". Not sure what's the difference. http://stackoverflow.com/questions/95554/overriding-a-mime-type-in-rails
ashitaka
For practical reasons (say for example you've got Flex's horrific HTTP client in the mix), sometimes you have to use 200 for everything. However, under normal circumstances, you want to use the most appropriate HTTP status code for the situation.
Bob Aman
+4  A: 

I prefer to reply with both an HTTP error status and application specific payload.