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?
200 and empty list of items. As the guest ressource in general is found but it has no matching items for your query
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:
The HTTP status codes are organized in classes. Have a look at this:
I would return 200 with an empty array...
Or a 204 (No Content)
A 404 is when a resource isn't found. Your resource in this case is the collection of guests... which exists.