Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP status code for a boolean resource

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?

like image 366
Marc Avatar asked May 06 '14 13:05

Marc


People also ask

What is a 201 status code?

The HTTP 201 Created success status response code indicates that the request has succeeded and has led to the creation of a resource.

What HTTP status code indicates a missing 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.

What is http200?

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.

Which HTTP status code is usually returned when a resource?

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.


1 Answers

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.

like image 119
jordan Avatar answered Sep 27 '22 03:09

jordan