Menu

Status codes

“The three-digit number that says how a request went.”

View .md

The first line of every response has a status code:

http
HTTP/1.1 404 Not Found

The 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.

RangeMeaningIn short
1xxInformational“Hold on…”
2xxSuccess“Here you go.”
3xxRedirection“Look over there.”
4xxClient error“You did something wrong.”
5xxServer error“I did something wrong.”

The ones worth memorizing#

CodeNameWhen
200OKIt worked, the result is in the body
201CreatedA POST created something. Often with a Location header pointing to it
204No ContentIt worked, nothing to send back (common after DELETE)
301Moved PermanentlyThe resource has a new URL forever
302FoundTemporarily somewhere else
304Not ModifiedYour cached copy is still good
400Bad RequestThe request is malformed (bad JSON, missing field)
401UnauthorizedYou’re not logged in
403ForbiddenYou’re logged in but not allowed
404Not FoundNothing at this URL
429Too Many RequestsSlow down (check the Retry-After header)
500Internal Server ErrorThe server crashed or hit a bug
503Service UnavailableDown 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?

Try it Send a request to a pretend API It runs in your browser and nothing leaves your computer, so break whatever you like.
api.example.com
Paths:

Request

Response

Press Send to see the answer.

Seeing only the status#

bash
curl -s -o /dev/null -w "%{http_code}\n" https://example.com/does-not-exist
text
404

Redirects in action#

-I asks for headers only. Notice the Location header that says where to go:

bash
curl -I http://github.com
http
HTTP/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):

js
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();

Byte check

0/3

Answer these 3 questions to commit this byte to your learning path.

  1. A status code starting with 4 means…
  2. You’re logged in, but not allowed to see the page. Which code fits?
  3. fetch gets a 500 back. What happens in your code?
Search posts and bytes

Optional account

Keep your progress everywhere

Everything on AminBytes is free to read, with or without an account. Sign in only if you want your commits, streak and merged topics saved, on every device.

or

We keep only your name, avatar and which bytes you finished, and you can delete it all anytime. Privacy