What You'll Learn
- The five families of HTTP status codes (1xx–5xx)
- The specific codes you will see in real API work
- How to choose the right success code for your API
- How to read error codes to debug faster
Why This Matters
Every HTTP response starts with a status code. It is the single most important piece of information in the response — it tells you whether your request worked, failed, or something in between. If you do not know what 401 vs 403 vs 422 mean, you will misdiagnose every API problem.
The Five Families
Status codes are three-digit numbers. The first digit tells you the family:
| Range | Family | Meaning |
|---|---|---|
| 100–199 | Informational | Rarely used in modern APIs |
| 200–299 | Success | The request worked |
| 300–399 | Redirection | The resource moved; client should follow |
| 400–499 | Client error | The client made a bad request |
| 500–599 | Server error | The server failed to handle a valid request |
Success Codes (2xx)
| Code | Name | When to use |
|---|---|---|
| 200 | OK | Generic success — GET, PATCH, sometimes POST/PUT |
| 201 | Created | POST created a new resource. Response body usually contains it. |
| 202 | Accepted | Request accepted for processing later (async). Common for webhooks. |
| 204 | No Content | Success but no body to return. Typical for DELETE. |
| 206 | Partial Content | Returned only part of the resource (range requests). |
The four you'll see daily
- 200 OK — "here's what you asked for"
- 201 Created — "I made the new thing you asked for"
- 204 No Content — "I did what you asked; nothing to return"
- 202 Accepted — "I'll do this later; check back"
Redirection Codes (3xx)
| Code | Name | When |
|---|---|---|
| 301 | Moved Permanently | The resource has a new permanent URL; update your bookmarks. |
| 302 | Found | Temporary redirect. Use the original URL next time. |
| 304 | Not Modified | Cached version is still valid; no body returned. |
Most APIs avoid 3xx — they prefer to return the resource directly or use a clear 404. But 304 is common in APIs that support caching via If-None-Match and ETag headers.
Client Error Codes (4xx)
This is the family you will spend the most time on. Every 4xx means the client did something wrong.
| Code | Name | What it means |
|---|---|---|
| 400 | Bad Request | Malformed syntax — invalid JSON, missing required field. |
| 401 | Unauthorized | Missing or invalid authentication. Log in first. |
| 403 | Forbidden | Authenticated, but you don't have permission for this resource. |
| 404 | Not Found | The URL doesn't match any resource. |
| 405 | Method Not Allowed | URL exists, but the method is wrong (e.g., DELETE on a list URL). |
| 409 | Conflict | Request conflicts with current state — duplicate email, version mismatch. |
| 410 | Gone | Resource existed but was permanently deleted (stronger than 404). |
| 415 | Unsupported Media Type | Wrong Content-Type header (e.g., sending XML to a JSON-only API). |
| 422 | Unprocessable Entity | JSON is valid but fails validation (e.g., email format wrong). |
| 429 | Too Many Requests | You hit the rate limit. Slow down or wait. |
The 401 vs 403 distinction
This is the most-misunderstood pair:
- 401 Unauthorized: "I don't know who you are." Missing or invalid token. Solution: authenticate.
- 403 Forbidden: "I know who you are, but you can't do this." Valid token, but no permission. Solution: request access from an admin.
If you are logged in but cannot delete a post, that is 403, not 401. If your token expired and the server does not recognize you, that is 401, not 403.
400 vs 422
- 400 Bad Request: The request itself is malformed — invalid JSON, missing
Content-Type, broken syntax. - 422 Unprocessable Entity: The request is well-formed JSON, but the values fail business rules (e.g.,
email: "not-an-email").
Many APIs use 400 for both cases. Strictly speaking, 422 is more accurate when the JSON parses but fails validation.
Server Error Codes (5xx)
5xx means the server failed. The client's request was valid; the server could not handle it.
| Code | Name | When |
|---|---|---|
| 500 | Internal Server Error | Generic "something broke on the server". |
| 501 | Not Implemented | Server doesn't support this method or feature. |
| 502 | Bad Gateway | A proxy/gateway got an invalid response from the upstream server. |
| 503 | Service Unavailable | Server is down for maintenance or overloaded. Try again later. |
| 504 | Gateway Timeout | A proxy/gateway did not get a response in time from the upstream server. |
What 5xx tells you as a client
5xx errors are usually not your fault. Retry with backoff, and if they persist, check the API's status page or contact the provider.
Common Mistakes
- Returning 200 for everything. Some APIs return 200 with an error in the body. This breaks standard HTTP clients and tools. Use the right status code.
- Using 401 for permission errors. If the user is authenticated but lacks permission, that is 403, not 401.
- Using 404 to hide authorization failures. Some APIs return 404 instead of 403 to avoid leaking that a resource exists. This is a deliberate security choice but should be documented.
- Treating 5xx as the client's fault. 5xx is the server's problem. Don't try to "fix" your request — retry with backoff.
- Forgetting the
Locationheader with 201. 201 Created should include aLocationheader pointing to the new resource's URL.
Practical Exercise (5 minutes)
Match each scenario to its correct status code:
- You send a POST with malformed JSON.
- You request a user ID that does not exist.
- You forgot to send your auth token.
- You send a valid token but try to delete another user's post.
- The server's database crashed mid-request.
- You successfully created a new user.
- You sent 100 requests in 10 seconds and the API blocked you.
Answers: 1→400, 2→404, 3→401, 4→403, 5→500, 6→201, 7→429.
Mini Challenge
Open the JSONPlaceholder API and trigger three different status codes intentionally: a 200 (GET any post), a 404 (GET a non-existent post ID like 999999), and a 201 (POST a new post). Use curl -i so you can see the status line.
Key Takeaways
- Status codes are three digits. The first digit tells you the family: 1xx info, 2xx success, 3xx redirect, 4xx client error, 5xx server error.
- Success: 200 (OK), 201 (Created), 204 (No Content), 202 (Accepted).
- Client errors: 400 (bad request), 401 (unauthenticated), 403 (forbidden), 404 (not found), 422 (validation failed), 429 (rate limited).
- 401 vs 403: 401 = "I don't know who you are"; 403 = "I know you, but you can't do this".
- 5xx = server's fault. Retry with backoff; don't try to "fix" your request.
Previously: Lesson 04 covered the five HTTP methods.
Today: You learned what the server sends back as a status code.
Next: In lesson 06 — HTTP Headers, you'll learn the metadata that travels alongside every request and response.
FAQ
Why does my browser show 304 sometimes?
The browser is using cached content. The server is saying "your cached copy is still valid; don't download it again." This is controlled by the If-None-Match and ETag headers, which we cover in lesson 06.
What status code should I use when a user's password is wrong?
401 Unauthorized. The user is attempting to authenticate, and they failed. Some APIs use 400, but 401 is more accurate because the response specifically indicates an authentication failure.
Comments
Comments
Post a Comment