In REST API resources: what can you return when a client requests a page that exceeds the maximum page number?

Let's say we only have 30 products and a REST API with QueryString parameters used for pagination. The example below pagesize

shows the amount of resources for page

.

api/products?page=1pagesize=10

      

What do you prefer to respond when the request requests a page above the maximum page as shown below?

api/products?page=125&pagesize=10

      

  • 400 - Invalid request?
  • 404 - Not Found?
  • 200 - Good (empty)?
  • 204 - Out of Content?
+3


source to share


2 answers


I would go with Not Found as the query syntax is fine. See status code explanations



+6


source


  • I don't think it's 400 because the request is valid, but there are REST APIs that use 400 because of out of range errors.
  • The 404 is valid if we say we don't need to bind every url matching the pagination pattern to a collection resource. By the type of range headers, it won't work because the pages there have the same URL. By page type, the query style is valid.
  • Active 200 with empty resource. The requested page contains no elements.
  • 204 should be used with empty message body. You will probably want to send backlinks to the first and last pages of the collection, so this may not help you.

So I think any of 200, 204 and 404 might be a valid solution.



  • 416 Range Requested Don't guess - maybe we could use this, which has the exact meaning we want, but it should only be applied with range headers. (note: should! = must, English - hard language: D)
+1


source







All Articles