5

We are implementing REST APIs in Java.

For GET REST endpoint, if I search for something:

  1. If any records/record found, then it will return 200 status code and results/result.

  2. If NO records found, what is the correct HTTP status code ?

When I searched in Google and SO, I found different answers like:

200 with empty list (if the API response is item but NOT list, then I can't send empty list)
204 No Content
404 Not Found

what is the correct HTTP status code if record not found or search results not found ?

3
  • 1
    You may consider using conventions of github api or youtube api. My opinion, i using 200 with empty list, same as above references.
    – huytmb
    Nov 29, 2018 at 16:43
  • If not finding anything is considered a client related error than return a 404, though in general this shouldn't be a failure per se IMO. 204 No Content is more or less intended to inform a client that it can remain its current state as the processed request didn't change anythng. It is therefore more often used with PUT operations. Hence I'd go for 200 OK Nov 29, 2018 at 16:44
  • 1
    Possible duplicate of Response status code for searches in REST APIs Nov 29, 2018 at 16:53

1 Answer 1

12

There are two different scenarios:

  • For retrieving a representation of a collection of resources (for example, GET /products?name=foo) that has no items, return 200 with an empty array.

  • For retrieving a representation of a single resource (for example, GET /products/1) that doesn't exist, return 404.

The 204 status code is meant to be used, for example, as result of a PUT operation, allowing the server to indicate that the action has been successfully applied to the target resource. It doesn't fit for the scenarios described above.


See this answer for further details.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.