Link Headers

Many HTTP APIs feature Link headers. They make APIs more self describing and discoverable.

GitHub uses these for pagination in their API, for example:

  1. >>> url = 'https://api.github.com/users/kennethreitz/repos?page=1&per_page=10'
  2. >>> r = requests.head(url=url)
  3. >>> r.headers['link']
  4. '<https://api.github.com/users/kennethreitz/repos?page=2&per_page=10>; rel="next", <https://api.github.com/users/kennethreitz/repos?page=6&per_page=10>; rel="last"'

Requests will automatically parse these link headers and make them easily consumable:

  1. >>> r.links["next"]
  2. {'url': 'https://api.github.com/users/kennethreitz/repos?page=2&per_page=10', 'rel': 'next'}
  3. >>> r.links["last"]
  4. {'url': 'https://api.github.com/users/kennethreitz/repos?page=7&per_page=10', 'rel': 'last'}