Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a REST API paginated resources: what would you return when a client request a page exceeding the maximum page number?

Let say we have only 30 products and a REST API with QueryString parameters used for pagination. In the exemple below pagesize is the number of resources per page.

api/products?page=1pagesize=10

What do you prefer to answer when the request is asking for a page over the max page like the following?

api/products?page=125&pagesize=10
  1. 400 - Bad Request?
  2. 404 - Not Found?
  3. 200 - Ok (with empty result)?
  4. 204 - No Content?
like image 530
Guillaume Raymond Avatar asked Sep 08 '14 12:09

Guillaume Raymond


People also ask

What is pagination limit?

The limit option allows you to limit the number of rows returned from a query, while offset allows you to omit a specified number of rows before the beginning of the result set. Using both limit and offset skips both rows as well as limit the rows returned.

What is REST API pagination?

You can paginate the JSON response that is called from the REST API. The order of the data is retained from page to page. Given the ability to paginate, you can quickly populate tables and make new REST calls every time you go to the next page of the data on the table.

What are two reasons for using REST API pagination?

The benefits of this approach is that it doesn't require additional backend logic. It only requires one limit URL parameter. It also features consistent ordering, even when new items are added to the database. It also works smoothly with large offset values.

How pagination is done in API?

The best way to build API pagination in a safe way is for the API to return a “cursor”, or opaque string, with the list of results. This token is usually called next or next_cursor or starting_after This token can be passed with the next API request to request the next page.


1 Answers

I would go with Not Found, since the request's syntax is perfectly fine. See the status code explanations

like image 131
PhilLab Avatar answered Sep 29 '22 10:09

PhilLab