I have come to a dilemma whilst developing an API at work. Say I have a route for a resource as follows:
/order/:orderId/returnable
This resource is supposed to check whether an order can be returned to a shop or not. Right now it is replying a status code of 200 if it IS returnable and 404 if not.
A colleague rightfully pointed that the resource is not gone and that I should use a 200 for any response, and then respond with a body containing my results.
Both have their semantic meaning, and I don't see a clear winner.
What response status code should we use? And should there be something in the body?
The HTTP 201 Created success status response code indicates that the request has succeeded and has led to the creation of a resource.
404: “The requested resource was not found.” This is the most common error message of them all. This code means that the requested resource does not exist, and the server does not know if it ever existed.
The HTTP 200 OK success status response code indicates that the request has succeeded. A 200 response is cacheable by default. The meaning of a success depends on the HTTP request method: GET : The resource has been fetched and is transmitted in the message body.
The origin server MUST create the resource before returning the 201 status code. If the action cannot be carried out immediately, the server SHOULD respond with 202 (Accepted) response instead.
Your colleague is right. From the spec (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5), it says 404 is when "The server has not found anything matching the Request-URI". In your case, the server has found something, which it needs to return. You'll need to define the syntax of the entity returned in responses to this API, and return HTTP 200. It can be as simple as a boolean value, or anything that makes sense as long as its documented.
If the given "orderId" does not exist, it should return HTTP 404.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With