Search code examples
resthttppaginationapi-design

REST API Status Code Pagination


I am currently writing an REST API using the Jersey Framework. I am following the HATEOAS principle and the user should only be moving through the api by the given links in my response body oder headers. On some Resources I have implemented pagination functionality. I was wondering though, what should I tell the User (HTTP Status Code), when he is not following my boundaries and just like randomly makes a request where the requested page is actually "out of bounds". Currently I just return a null Collection, but I think as a User, I wouldn't be able to make something out of such a response. I considered using the Status Code "Not Found", but I am not sure if that is the appropriate one. I really want to stay true to REST and that implicates I stay true to HTTP. So can anyone give me suggestions or even tell me if there is actually a rule for my problem?

Maybe a concrete example:

  • page size = 10;
  • Collection.size = 27;
  • requested page = 4;
  • Paging starts with page 0

, so requesting http://...../resource?page=0, returns the first page. My question is, what should I return for the request http://...../resource?page=4? Currently I am just returning null, but I don't think that's the proper response. Thanks in advance

EDIT: I am only asking about the expected Response in case the requested page is "empty". I know that fixed page size design may be doomed for future change requests, but since this API is part of a Microservice, there will be none, except we have a fight inside our team :)


Solution

  • 404 not found is the appropriate return code for an out of bound access.

    But you should consider change your resource identifier (URI). Returning 404 or 200 depending on a URL parameter is not a good design.

    It would be better to treat every page as single resource. That's also true for HATEOAS.

     .../resource/page/0  #200 + return link in header to next resource  .../resource/page/1
     .../resource/page/4  #404 + return link to first resource  .../resource/page/0)
    

    Using URL parameters should be the last option if nothing else works (for example range access).