Skip to content

Pagination

Joakim Skoog edited this page Jan 24, 2024 · 1 revision

An API of Ice And Fire provides a lot of data about the world of Westeros. To prevent our servers from getting cranky, the API will automatically paginate the responses. You will learn how to create requests with pagination parameters and consume the response.

Things worth noting:

  1. Information about the pagination is included in the Link header
  2. Page numbering is 1-based
  3. You can specify how many items you want to receive per page, the maximum is 50

Request

You specify which page you want to access with the ?page parameter, if you don't provide the ?page parameter the first page will be returned. You can also specify the size of the page with the ?pageSize parameter, if you don't provide the ?pageSize parameter the default size of 10 will be used.

Let's make a request for the first page of characters with a page size of 10. Since we're only interested in the pagination information we provide the -I parameter to say that we only care about the headers.

$ curl -I "https://www.anapioficeandfire.com/api/characters?page=1&pageSize=10"

Response

Link: <https://www.anapioficeandfire.com/api/characters?page=2&pageSize=10>; rel="next", <https://www.anapioficeandfire.com/api/characters?page=1&pageSize=10>; rel="first", <https://www.anapioficeandfire.com/api/characters?page=214&pageSize=10>; rel="last"

Possible link types

  • next - Next page of results
  • prev - Previous page of results
  • first - First page of results
  • last - Last page of results

These links can then be used to navigate to other pages of results.

Clone this wiki locally