Talking to an API · practice
Separate URL, Parameters, and Headers
An params= and request metadata through headers=.
The items route is /items. Ask for page 2 with a data structure:
document = request_json("/items", params={"page": 2, "page_size": 2})
Requests encodes those values into the /v1/items and the query is ?page=2&page_size=2. The query selects which page to return; the header below supplies metadata about the request. Your code explicitly supplies only the documented practice metadata:
PRACTICE_HEADERS = {"X-Practice-Name": "python-in-practice"}
Do not add authorization, tokens, host overrides, or public URLs. This service does not define them.
The response includes an 200 means success, 404 means the requested resource was not found, and 500 means a server error. The body can contain
Before Lesson 3 turns unsuccessful statuses into request_json visible: read response.status_code, then decode JSON. The
status = response.status_code
document = response.json()
return document
Reading a status is not the same as handling it. That temporary line lets you inspect the response boundary without pretending a 404 envelope is successful data. If you manually call /headers, its practice-header field should echo the fixed value. Requests can add ordinary client and protocol headers of its own; they are not extra application metadata chosen by your code.
A fake response can record the order in which your code touched it. That is worth knowing, because it means “did I check the status before decoding?” is a question a test can answer exactly, rather than something you have to take on faith while reading your own code.
Where should the requested page number go?
Task
Keep request_json to one fixed-base GET. Forward unseen params unchanged, send exactly X-Practice-Name: python-in-practice, keep the timeout explicit, and observe response.status_code before calling response.json().