The Standard Client and Status Classes
Decide by Class First
get_json returns whatever status arrived. On the first page of items that is 200, but the same
Five classes
A
| Codes | Class | What it tells the client | What the client does next |
|---|---|---|---|
| 1xx | informational | the server has more to say; this is not the answer yet | wait for the final response that follows |
| 2xx | success | the request worked | use the response |
| 3xx | redirection | the answer is somewhere else | look at the Location header; Chapter 6 follows it |
| 4xx | client error | something about the request was wrong | change the request before sending it again; for 429 Too Many Requests, wait at least as long as Retry-After says |
| 5xx | server error | the server failed to answer a request that may be fine | trying again later may work; this course does not retry |
The class answers the first question every client has: did it work, and if not, whose move is it? A 4xx says the next move is yours, because the server will not act until something about the request, or when you send it, changes. A 5xx says the server failed; your request may have been fine. The exact code adds detail inside the class. A 404 says the thing you asked for is not there, 401 and 403 are about who you are and what you may do (Chapter 7 tells them apart), and 301, 307, and 308 say how a redirect should be followed (Chapter 6).
Why the class comes first
HTTP’s definition (RFC 9110, section 15) asks every client to understand the class of any code, and to treat a code it does not recognize like the x00 code of its class. A client that has never heard of 299 treats it as 200, and one that has never heard of 451 treats it as 400. So a client that decides by class first is never stuck: it has an answer for every code, including codes invented after it was written. It can still look at the exact code afterward, as it will for 429.
Finding the class takes one operation you know from Python Fundamentals: floor division by 100 drops the last two digits. Once you have the class, one comparison answers “did it work?”:
Only 204 prints True: it is the one success among the four, even though it carries no body.
What raise_for_status did
In Python in Practice, response.raise_for_status() turned every 4xx and 5xx into an
Your client receives 299, a code it has never seen. What should it do?
A response says 429 Too Many Requests. Which class is it, and what should a client do next?
What does this print?
for code in [101, 304, 451, 500]:
print(code, code // 100)
You now have a rule for any status. Some decisions need more than the status, such as how long to wait after a 429, and that information arrives in the