Re:I've been abusing HTTP Status Codes in my APIs for years
by m3galinux on Wednesday July 13, 2022 @01:30PM
(#32085964)
Completing a "search" SHOULD return a 200 with an empty results set. But a search is "/api/employees?name=Bob", not /api/employees/1199. The former is an endpoint that exists but was unable to find data: it should return the correct data structure normally returned for searches, but with no results. The latter is a direct link to a particular resource, which should 404 if it doesn't exist (as if any other file at a particular path doesn't exist).
2XX - Request was understood and we found what you were looking for
can be broken down into two parts:
1. Request was understood
2. We found what you were looking for.
This guy seems to be advocating that completing a search and there being no results is actually a successful request, and so should be responded to with 200.
The problem though is that the spec (RFC7231) requires a representation of the resource to be sent in response to a GET.
If there's no payload, then you can send a 204 instead, but this raises its own challenges - a 204 just means that the server is not sending content, not that no content was found (which is a subset of 'not sending content').
I think on balance, 404 is correct - the spec indicates that it should be used where there's no current representation, which I think is an accurate description of a failed search.