DEV Community

Discussion on: 404 status code! Really?

Collapse
mdamaceno profile image
Marco Damaceno Author

Hi Sorin Costean!

I don't think so. If you have an endpoint to get a list of books and make a request to it. What would it be that response? A empty array with 200 status code or 204 with no content? I think the first response fits more the client expectations than the second one. Why would it be different for a single resource?

Collapse
rafaacioly profile image
Rafael Acioly • Edited on

Marco, what i usually do is

If the request came on a list endpoint and there's no content i return 200 with a empty list, if the request is for a single record and the record doesn't exist i return 204.

Normally my "pagination" responses retrieve another useful data as well like "next_page", "count" and etc, so it makes easy for validation if the response is "200".

Thread Thread
mdamaceno profile image
Marco Damaceno Author

An empty list is no content as well. Why not use 204 for it?

Thread Thread
pchinery profile image
Philip

If you have no items for the list, returning an empty list will yield the full available content. Maybe you will also add some metadata (e.g. total number of entries) to support pagination. Your consumer will not have to treat this differently from a filled list. When requesting a single item that does not exist, users would have to examine the content and see if they can work with it. And from my experience: if you start seeing surprising responses, the trust in the responses from this system drops rapidly.