HTTP 200 or 404 for an empty list?

I know this is a fairly common question, but I did not find an answer that satisfies me.

I have been using the django rest framework for a long time, but it basically does not matter, except for the above example. Its default behavior is to return HTTP 200 with an empty list resource when accessing a route with an empty list of elements. For example: if we had a route, for example /articles/ , to access the list of articles, but there were no elements in it, we would get an answer like the following json:

 {"count":0, "next":null, "previous":null, "items": []} 

It's fine. We found the resource we were looking for in / articles /, it just doesn’t have any elements.

If we get access to the /articles/?page=1 route, we get the same answer.

So far so good. Now we are trying to access /articles/?page=2 , and the response code changes. Now get 404 as if the resource could not be found with an error message stating that the page does not contain results. Is this the same case as with? Page = 1 ...

I was fine with this behavior, but today I started asking about this design. What is the case ?page=1 different from ?page=2 ? And what else, how could you say if the request was “valid” when issuing the HEAD request? Acts in the sense of the content of any results.

This can be useful in cases such as filtering a list that checks for the availability of a specific field (for example, issuing a HEAD request to /users/?username=ted ).

  • A response of 200 explicitly means that the request was understood and the elements were found.
  • A 404 will mean that the request was understood, but no elements were found at this location / URI (AFAIK request parameters are also part of the URI).
  • If the query cannot be understood, 400 will be returned for syntax errors and 422 for semantic errors.

Is this a good design? Why do most people seem to disagree with this, and what are the flaws in it?

+5
source share
3 answers

I would go for 200 because the articles resource. When ted requested by users the same applies, users is a resource, and as soon as it is there, 200 is fine from my point of view. If GET users/ted a 404 would be as good as 410 (GONE) , if a user named ted were in the past (could be better applied to articles than users).

+8
source
  1. Because we are fine with a blank page1, not a blank page2.

This is due to consideration of the user interface (page1 must exist!), However, it solves the response code.

+1
source

200 just means the request succeeded. The information returned with the response depends on the method used in the request, for example: a GET object corresponding to the requested resource is sent in response;

HEAD fields of the header object corresponding to the requested resource are sent in the response without any message body;

POST object describing or containing the result of the action;

TRACE is an entity containing the request message received by the destination server. 404 - The server did not find anything matching the request-URI. In this case, I think this means that we did not find the page on which the articles would be published. The way it is. - but, perhaps, understand what is being returned, and format the message about the return of 0 articles.

0
source

All Articles