Using HTTP status codes to reflect success/failure of Web service request?

Posted by jgarbers on Stack Overflow See other posts from Stack Overflow or by jgarbers
Published on 2010-03-22T22:20:34Z Indexed on 2010/03/22 22:31 UTC
Read the original article Hit count: 320

Filed under:
|
|
|
|

I'm implementing a Web service that returns a JSON-encoded payload. If the service call fails -- say, due to invalid parameters -- a JSON-encoded error is returned. I'm unsure, however, what HTTP status code should be returned in that situation.

On one hand, it seems like HTTP status codes are for HTTP: even though an application error is being returned, the HTTP transfer itself was successful, suggesting a 200 OK response.

On the other hand, a RESTful approach would seem to suggest that if the caller is attempting to post to a resource, and the JSON parameters of the request are invalid somehow, that a 400 Bad Request is appropriate.

I'm using Prototype on the client side, which has a nice mechanism for automatically dispatching to different callbacks based on HTTP status code (onSuccess and onFailure), so I'm tempted to use status codes to indicate service success or failure, but I'd be interested to hear if anyone has opinions or experience with common practice in this matter.

Thanks!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about JSON