Beginner · How the web and backends work · topic 4 of 54 · 20 min
HTTP status codes
Explanation
Five families
A status code is a three-digit number at the start of every response. The first digit tells you the family, so even an unknown code like 418 can be understood: it starts with 4, so the client did something wrong.
| Family | Meaning | Example |
|---|---|---|
| 1xx | Information: "keep going" | 100 Continue (rare in daily work) |
| 2xx | Success | 200 OK |
| 3xx | Redirect: look somewhere else | 301 Moved Permanently |
| 4xx | Client error: the request was wrong | 404 Not Found |
| 5xx | Server error: the server failed | 500 Internal Server Error |
Codes you will use every day
| Code | Name | When to use it |
|---|---|---|
| 200 | OK | Normal success with a body (GET, PATCH) |
| 201 | Created | A POST created something new |
| 204 | No Content | Success with no body, often after DELETE |
| 301 | Moved Permanently | The page moved for good (old URL to new URL) |
| 302 | Found | A short-time redirect, for example to the login page |
| 304 | Not Modified | The copy the browser already has is still fresh |
| 400 | Bad Request | The request is broken, like invalid JSON |
| 401 | Unauthorized | Not logged in: we do not know who you are |
| 403 | Forbidden | Logged in, but not allowed to do this |
| 404 | Not Found | The thing does not exist |
| 405 | Method Not Allowed | The path exists, but not with this method |
| 409 | Conflict | Clashes with current data, like a username already taken |
| 422 | Unprocessable Content | Well-formed data that breaks a rule, like age -5 |
| 429 | Too Many Requests | Slow down, for example too many OTP requests |
| 500 | Internal Server Error | A bug or crash in the server code |
| 502 | Bad Gateway | A server in the middle got a bad answer from the next server |
| 503 | Service Unavailable | The server is down for maintenance or overloaded |
401 or 403?
function family(code) {
const first = Math.floor(code / 100);
return first + 'xx';
}
console.log(family(201)); // 2xx
console.log(family(404)); // 4xx
console.log(family(503)); // 5xxCodes 429, 502 and 503 are often temporary. A client can wait a little and try again. A 400 or 404 will fail the same way every time, so retrying does not help.
Watch the lesson
HTTP status codes · 1/6
Every response starts with a three digit status code. It tells the client what happened.
Prefer a video by a teacher? Find free videos on “http status codes explained 200 404 500 for beginners” on YouTube ↗
In real life: An OTP login flow
A user asks for an OTP: the server sends it and answers 200. The user asks again ten times in a minute: the server answers 429 Too Many Requests. The user types a wrong OTP: 401. The user is logged in but opens another user's order: 403. The order number does not exist: 404. The SMS provider is down: the backend answers 503 so the app can show "Please try again in a few minutes".
const attempts = { '9876543210': 11 };
function requestOtp(mobile) {
if (attempts[mobile] > 5) return { status: 429, body: 'Too many requests. Try after 10 minutes.' };
return { status: 200, body: 'OTP sent' };
}
console.log(requestOtp('9876543210')); // status 429
console.log(requestOtp('9123456780')); // status 200Practice (0/3 done)
Write a function statusFamily(code) that returns "informational" for 100-199, "success" for 200-299, "redirect" for 300-399, "client error" for 400-499, "server error" for 500-599 and "unknown" for anything else.
Write a function chooseStatus(situation) that returns the right code: "ok" 200, "created" 201, "deleted" 204, "bad json" 400, "not logged in" 401, "not allowed" 403, "not found" 404, "already exists" 409, "too many requests" 429. Anything else returns 500.
Write a function isRetryable(status) that returns true for codes worth trying again later (429, 502, 503, 504) and false for every other code.
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.