- All Courses
- Python Foundations for Data Work
- Files, APIs, and the Standard Library
- Calling HTTP APIs
Calling HTTP APIs
Parsing the body before checking the status is the most common mistake: an error page is not JSON, and the traceback you get points at the parser rather than the failed request.
Overview
The network is not a function call
Parsing the body before checking the status is the most common mistake: an error page is not JSON, and the traceback you get points at the parser rather than the failed request.
Set a timeout on every request. Without one, a hung server hangs your script indefinitely, and there is no default that saves you.
Transient failures deserve a retry with exponential backoff; a client error does not. Retrying a bad request just sends the same wrong thing repeatedly.
- Check the status code first
- Set a timeout on every call
- Retry only on transient failures, with backoff
In this lesson you will:
- Send a request and read the response
- Check the status before parsing the body
- Retry transient failures with backoff
Resources
Notes are not saved yet — they clear when you leave this page.
Reading and writing CSV and JSON
16m
Dates, paths, and the standard library
35m