QuestionQ495

Using APIs

Drag the code snippets from below to the blanks in the Python script to consume REST API pagination. Not every option will be used.

Drag & Drop
response.links
response.code
destination
response.headers.get
response.headers.post
response.status_code
def process_all_pages(url):
    data = []
    try:
        response = requests.get(url)
        if  == 200:
            while ('Link'):
                response = requests.get(['next']['url'])
                response.raise_for_status()
                data.append(response.json())
        return data
    except Exception as e:
        print('Server returned non-200 OK response during pagination')
Explanation

In the Requests library, Response.status_code is the numeric HTTP status code. Response.links parses RFC Link headers into a dictionary keyed by relation names, so the next relation can be followed with response.links['next']['url']. The raw Link header must first be retrieved with response.headers.get('Link') to test whether pagination information is present.

Community Discussion

No comments yet. Be the first to start the discussion!