Menu
Status codes
“The three-digit number that says how a request went.”
The first line of every response has a status code:
HTTP/1.1 404 Not FoundThe first digit tells you the category, so you can understand codes you’ve never seen before.There’s even 418 I'm a teapot, from an April Fools’ joke spec in 1998. Some servers really return it.
| Range | Meaning | In short |
|---|---|---|
| 1xx | Informational | “Hold on…” |
| 2xx | Success | “Here you go.” |
| 3xx | Redirection | “Look over there.” |
| 4xx | Client error | “You did something wrong.” |
| 5xx | Server error | “I did something wrong.” |
The ones worth memorizing#
| Code | Name | When |
|---|---|---|
200 | OK | It worked, the result is in the body |
201 | Created | A POST created something. Often with a Location header pointing to it |
204 | No Content | It worked, nothing to send back (common after DELETE) |
301 | Moved Permanently | The resource has a new URL forever |
302 | Found | Temporarily somewhere else |
304 | Not Modified | Your cached copy is still good |
400 | Bad Request | The request is malformed (bad JSON, missing field) |
401 | Unauthorized | You’re not logged in |
403 | Forbidden | You’re logged in but not allowed |
404 | Not Found | Nothing at this URL |
429 | Too Many Requests | Slow down (check the Retry-After header) |
500 | Internal Server Error | The server crashed or hit a bug |
503 | Service Unavailable | Down for maintenance or overloaded |
Tip
401 vs 403: 401 means “who are you?” (log in and try again). 403 means “I know who you are, and the answer is no.”
Make the codes happen#
Each button combination below leads to a different status. Can you get a 201, 204, 401, 404, 405, 406 and 415?
Seeing only the status#
curl -s -o /dev/null -w "%{http_code}\n" https://example.com/does-not-exist404Redirects in action#
-I asks for headers only. Notice the Location header that says where to go:
curl -I http://github.comHTTP/1.1 301 Moved Permanently
Location: https://github.com/Add -L and curl follows the redirect for you, just like a browser does.
Handling status codes in code#
fetch only throws on network errors. A 404 or 500 is still a “successful” fetch, so check res.ok (true for 200–299):
1const res = await fetch("/api/todos/999");
2if (!res.ok) {
3 throw new Error(`Request failed: ${res.status} ${res.statusText}`);
4}
5const todo = await res.json();Answer these 3 questions to commit this byte to your learning path.
Reset. This byte isn't committed any more. Answer the questions again to commit it.
$ git commit -m "learn(http): Status codes"
[main 4f89ac1] learn(http): Status codes
1 byte changed, 3 insertions(+)
See it on your path Already committed . Try the questions again any time, it won't undo anything. To uncommit it and start over: