Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Response status code for searches in REST APIs

Suppose, we have the following API:

GET /api/guests/{id}
GET /api/guests?name=dummy

Which status code should I return when there are no guests matching criteria? I mean no guests with name dummy.

Should it be 404, 204 or 200 with an empty array?

like image 749
StasKolodyuk Avatar asked Jan 22 '16 11:01

StasKolodyuk


People also ask

What is status code 404 in API?

404 is a status code that tells a web user that a requested page is not available. 404 and other response status codes are part of the web's Hypertext Transfer Protocol response codes. The 404 code means that a server could not find a client-requested webpage.

What does the HTTP status code 204 represent in a REST service?

The HTTP 204 No Content success status response code indicates that a request has succeeded, but that the client doesn't need to navigate away from its current page.

How do I get response status from API?

To get the status code of an HTTP request made with the fetch method, access the status property on the response object. The response. status property contains the HTTP status code of the response, e.g. 200 for a successful response or 500 for a server error.


1 Answers

The proper status code for each situation

Consider the following situations:

  • GET /api/guests?name=dummy

It's an operation that requests a representation of a collection. If no items match the search criteria, return a 200 status code with an empty array in the response body, indicating that the request was successfully received, understood, and accepted by the server, and collection itself exists but the search returned no results.

  • GET /api/guests/{id}

It's an operation that requests representation of a single resource using its unique identifier. If no resource was found return a 404 error, indicating that the server did not find the resource with that identifier.

More details

Have a look at the status code definitions in the RFC 7231 (which updates the old RFC 2616). It's pretty clear:

6.3.1. 200 OK

The 200 (OK) status code indicates that the request has succeeded. The payload sent in a 200 response depends on the request method. For the methods defined by this specification, the intended meaning of the payload can be summarized as:

  • GET: a representation of the target resource;

  • HEAD: the same representation as GET, but without the representation data;

  • POST: a representation of the status of, or results obtained from, the action;

  • PUT, DELETE: a representation of the status of the action;

  • OPTIONS: a representation of the communications options;

  • TRACE: a representation of the request message as received by the end server.

[...]

6.3.5. 204 No Content

The 204 (No Content) status code indicates that the server has successfully fulfilled the request and that there is no additional content to send in the response payload body. Metadata in the response header fields refer to the target resource and its selected representation after the requested action was applied.

[...]

6.5.4. 404 Not Found

The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource or is not willing to disclose that one exists. A 404 status code does not indicate whether this lack of representation is temporary or permanent; the 410 (Gone) status code is preferred over 404 if the origin server knows, presumably through some configurable means, that the condition is likely to be permanent.

[...]

The HTTP status codes are organized in classes. Have a look at this:

6.3. Successful 2xx

The 2xx (Successful) class of status code indicates that the client's request was successfully received, understood, and accepted.

6.5. Client Error 4xx

The 4xx (Client Error) class of status code indicates that the client seems to have erred. Except when responding to a HEAD request, the server SHOULD send a representation containing an explanation of the error situation, and whether it is a temporary or permanent condition.

[...]

like image 164
cassiomolin Avatar answered Oct 26 '22 21:10

cassiomolin