Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What HTTP return code in the request for empty result 204 or 404?

Tags:

rest

http

I have a call with a return object, but this call may return an empty result.

My URL in controller is:

/api/find-by-id/{id}

This controller make calls service as the following:

service.findById(Integer id);

My sevice searches through DAO:

repository.findById(Integer id); 

When DAO return an empty value, should I send error HTTP 404 or HTTP 204 to front?

Considering:

  • 2xx: Success - The action was successfully received, understood, and accepted

  • 4xx: Client Error - The request contains bad syntax or cannot be fulfilled

HTTP 204: Means that something was found, but it's empty.

HTTP 404: Not found.

like image 417
Thiago Marcello Avatar asked Sep 13 '25 20:09

Thiago Marcello


1 Answers

If your findById always returns exactly 0 or 1 items, and this is your request:

GET /api/find-by-id/{id}

Then I believe that the result should be 404 if an item does not exist.

204 is different. It doesn't really imply an item doesn't exist, it implies that the operation was a success, but the result is empty + a hypermedia client should not refresh the current view. It's rarely used as a response to GET request because it doesn't make that much sense.

Since it sounds like you're using a database, the equivalent to 204 / empty would be a database record with that id, but all the fields are null. The equivalent to 404 would be that the database record does not exist at all.

like image 174
Evert Avatar answered Sep 17 '25 03:09

Evert