DEV Community

Discussion on: Why some API response codes are subjective ?

Collapse
 
philipalexanderwallin profile image
PhilipAlexanderWallin • Edited

If you want to return an empty array you shouldn't use 204, as it goes against specification (tools.ietf.org/html/rfc7231#sectio...
"A 204 response is terminated by the first empty line after the header fields because it cannot contain a message body."

From what I have read and believe is best practice my interpretation is:
404 for a collection, for example /products: the collection doesn't exist
404 for a resource, for example /products/1: the resource doesn't exist
If the collection do exist, which it should in most cases, and it is empty, it should return an empty array with 200 OK.

Collapse
 
shaijut profile image
Shaiju T

Thanks, When you say collection do you mean Table in Database ?

Also based on filter if search: /products?search= returns empty array then should I return 200 OK ?

Collapse
 
philipalexanderwallin profile image
PhilipAlexanderWallin

Usually a list of resources in a REST API is referred to as a collection.

I would say 200 OK is correct here, some might argue that it should be 404, but I believe that is semantically incorrect. You did a request for resources, the search went fine, but you found 0 resources to return. 404 would to me mean that the concept "products" doesn't exist, which would make sense if it was a dynamic collection which was yet to be created. Returning an empty list also makes the life of the consumer of the API easier, as he would otherwise need to have a special case for no results found.