Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP GET Request Status 204 Vs 404

I have 2 resources User and Album. An user has a list of albums. To get albums there are 2 REST API.

  1. user/{userId}/albums/{albumId} gets album by albumId. If not found returns 404
  2. user/{userId}/albums gets all albums by userId. In this case, if a user has no albums, what should be the status code 204 or 404?
like image 593
Navrattan Yadav Avatar asked Dec 16 '15 12:12

Navrattan Yadav


People also ask

Can get return 204?

The 204 (No Content) status code indicates that the server has successfully fulfilled the request and that there is no additional content to send in the response payload body. While 200 OK being a valid and the most common answer, returning a 204 No Content could make sense as there is absolutely nothing to return.

When should 204 be used?

The HTTP 204 No Content success status response code indicates that a request has succeeded, but that the client doesn't need to navigate away from its current page. This might be used, for example, when implementing "save and continue editing" functionality for a wiki site.

Is 204 no content an error?

HTTP Status 204 (No Content) indicates that the server has successfully fulfilled the request and that there is no content to send in the response payload body.

Should I return 404?

Based on the code above, is it correct to return a NOT_FOUND status ( 404 ), or should I be returning 204 , or some other more appropriate code? If you would expect a resource to be there, because it is looked up using an ID, then a 404 is expected. Something goes wrong, the resource you need to be there is not found.


1 Answers

Is the absence of any album really seen as an error? Assuming the albums are returned as a JSON array, the common response to such a situation would be a HTTP 200 with an empty array as the body.

Returning 404 signals that the resource doesn't exist, kind of saying that it isn't even possible to ask for the list of albums for this particular user. But in fact, it's possible to successfully return the list of albums, it's just that the list is empty. It doesn't seem at all exceptional to me. This is completely in contrast to retrieval of one specific album using an ID that doesn't exist (using your other endpoint); in such a situation a 404 is correct.

While a 204 seems better than a 404, because it at least tells the client that the request was successful but had no content, its intention is not really to be used to signal a "successful absence". Rather, it signals that the resource DOES exist but for some reason the server chose not to include the resource in the response body - for example the purpose of the request could have been to simply pass back some headers to the client.

A 204 can also be used as a response to a POST request where some action was carried out by the server without necessarily creating any new resource (which would have implied a 201 CREATED), or where it's for some other reason not relevant to return any resource.

I think it's clear that what you need is a

GET /user/xxx/albums  HTTP/1.1 200 OK  [] 
like image 150
JHH Avatar answered Oct 12 '22 22:10

JHH