DEV Community

Shaiju T
Shaiju T

Posted on

Why some API response codes are subjective ?

Lets say I have below search API.

METHOD  URL          STATUS  RESPONSE

GET     /products    204     []
Enter fullscreen mode Exit fullscreen mode

Isn't it good to use 204 status code when the API returns empty results ?

I looked at some answers in StackOverflow:

  1. What is the proper REST response code for a valid request but an empty data?

  2. HTTP status code for a REST search without result

Some say to use 400 and some say 204 or 200.

What do you think ? Is there any authoritative source to refer ?

Top comments (8)

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.

Collapse
 
anjankant profile image
Anjan Kant • Edited

200 Status code is the best practice while the search data or empty result set, if you pass any other like 400 status code - The most generic code out of the 4xx series **which indicates that the **request received is a bad one so 400 status code can't apply and moreover SEO viewpoint it's also bad practice. you may go deeply about status code with following link Status codes

Collapse
 
soumyabrata1147 profile image
Soumyabrata Bhattacharjee

I think 200 is the best status code for this.Your are getting an empty result because there is no data to show according to your search value.It is not an error.So 200 is absolutely okay.I use 200 for search results.I also set a variable named "status".So when it returns a status=1 i.e its containing at least one row and returns status=0 in case no data found.I think this will help you.😊

Collapse
 
shaijut profile image
Shaiju T

@ben , What does dev.to API return if there is empty result ?

Collapse
 
alenvarazdinac profile image
Alen Varazdinac

I would go for 200 since the 204 is mostly used for PUT requests.
400 is not used in these cases like this and can hurt your SEO.

Collapse
 
juniordevforlife profile image
Jason F

Return a 418. Honestly though, I think returning a 200 with the empty array is valid.