# Status codes

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

Source: https://aminbytes.dev/bytes/http/responses/status-codes/


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. (Side note: 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`?

*[Interactive example: try it at https://aminbytes.dev/bytes/http/responses/status-codes/]*

## 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 {hl_lines="2-4"}
const res = await fetch("/api/todos/999");
if (!res.ok) {
  throw new Error(`Request failed: ${res.status} ${res.statusText}`);
}
const todo = await res.json();
```


## Byte check

1. A status code starting with **4** means…
   - The server crashed
   - **The client did something wrong** (correct)
   - Look somewhere else
   4xx: *you* did something wrong. 5xx: *the server* did.

2. You're logged in, but not allowed to see the page. Which code fits?
   - `401`
   - **`403`** (correct)
   - `404`
   401 means "who are you?". 403 means "I know who you are, and the answer is no."

3. `fetch` gets a `500` back. What happens in your code?
   - `fetch` throws an error
   - **It resolves normally and `res.ok` is `false`** (correct)
   - The browser retries automatically
   `fetch` only throws on network errors, so always check `res.ok`.


