ETExamTower
Q47Using APIs

Refer to the exhibit. Drag the Python code parts from the left to the numbered items on the right to complete the missing sections of the exhibit that uses REST API pagination. <DragDrop items={["response.status_code","response.links['next']['url']","response.headers.get('Link')"]} slots={[{"answer":"response.status_code","id":"item1"},{"answer":"response.links['next']['url']","id":"item2"},{"answer":"response.links['next']['url']","id":"item3"}]} template={"def process_all_pages(url):\n data = []\n try:\n response = requests.get(url)\n if {{item1}} == 200:\n while {{item2}}:\n response = requests.get({{item3}})\n response.raise_for_status()\n data.append(response.json())\n return data\n\n except Exception as e:\n print(\"Server returned non-200 OK response during pagination\")"} reuse={true} explanation={"`Response.status_code` provides the HTTP status value, so comparing it to 200 tests for an OK response. Requests parses Link headers into `Response.links`; the `next` relation’s `url` value identifies the next page. That URL is used both to continue pagination and as the argument for the next request.\n\n**Learn more:** [Requests: Response API](https://requests.readthedocs.io/en/latest/api/#requests.Response)"} />

Question exhibit
← → navigate · a answer
Discussion · 6
35
I agree with the answer: 1. response.status_code 2. response.headers.get('Link') 3. response.links['next']['url']
7
FR99 is correct. The correct URL for the next page can be obtained by using response.links['next']['url']. The use of response.headers.get('Link') could easily be replaced with an infinite loop like 'while True:' and it would be the same. They're just testing our knowledge here. The trick is that once we reach the last page, response.links['next']['url'] inside the 'try' context will fail and we'll move to the 'except' clause and the program will exit.
4
That's true, but it really should be response.links.get('next') since that checks whether there is a next link.
3
I agree with FR99
2
Correct: 1. response.status_code 2. response.headers.get('Link') 3. response.links['next']['url']
1
The answer is correct: 1. response.status_code 2. response.headers.get('Link') 3. response.links['next']['url']