APIs and Async · HTTP and JSON · lesson 2 of 12
Status codes
about 14 minutes · free · runs in your browser
What the number means
The status code is the server telling you which of you has a problem.
| Range | Meaning | Whose problem |
|---|---|---|
| 2xx | it worked | — |
| 3xx | it moved | usually handled for you |
| 4xx | your request was wrong | yours |
| 5xx | the server broke | theirs |
The ones worth knowing by name: 200 OK, 201 Created, 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 429 Too Many Requests, 500 Internal Server Error, 503 Service Unavailable.
The distinction that matters operationally: 4xx will fail again if you retry it — your request is wrong and repeating it changes nothing. 5xx and 429 are worth retrying, because the server might recover.
response.raise_for_status() turns a 4xx or 5xx into an exception, which is useful
when you would rather crash than continue with nonsense.
Your turn: write classify(status) returning "ok", "client error",
"server error" or "other", and should_retry(status) returning True only for
5xx and 429.
You start from this, and edit it in the browser:
def classify(status):
pass
def should_retry(status):
pass