# Status Codes and Headers

A response's three-digit status code tells the client how its request went before it reads a word of the body, and headers such as Content-Type tell it how to read that body.

Apprentice · Node.js · REST API · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Choose the status code a server should send, such as 200, 201, 204, 400, 401, 403, 404, 409 or 500
- Explain what headers tell the other side, such as Content-Type for a JSON body

## Key takeaways

- A status code's first digit is its family: 2xx it worked, 3xx look somewhere else, 4xx the request has a problem, and 5xx the server broke.
- The codes an API sends most are 200 OK, 201 Created, 204 No Content, 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 409 Conflict and 500 Internal Server Error.
- A client decides what to do from the status code, so a server that answers 200 to everything leaves the client guessing.
- Headers are name: value notes on a request or response. Content-Type: application/json says the body is JSON.
- In a .http file, the headers go under the request line, then a blank line, then the body.

Every app you use checks a response's status code before anything else. It's how a shopping app knows to show "Item not found" instead of an empty page, and how a sign-in form knows the password was wrong. The servers you build will send these codes, and the apps that call them will trust what they say.

Here's why that matters. Ask the tiny garden server from *HTTP in Plain Words: Requests and Responses* for a plot that doesn't exist, `GET http://localhost:3100/plots/99`, and REST Client shows:

```text
HTTP/1.1 200 OK
Content-Type: application/json
Date: Tue, 17 Nov 2026 17:10:35 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Transfer-Encoding: chunked

[{"number":1,"size":"small","gardener":"Rosa","growing":"tomatoes"},{"number":2,"size":"small","gardener":null,"growing":null},{"number":3,"size":"large","gardener":"Malik","growing":"pumpkins"}]
```

`200 OK` says "here's what you asked for", and then comes every plot. A phone app would believe plot 99 exists. The server should have said "there's nothing at that address" instead, and status codes are how it says so.

## Three digits that say how it went

Think of a clerk's first words when you reach the counter. "Here you go." "Try the counter next door." "Sorry, you filled in the form wrong." "Sorry, our oven broke." You know how it went before they hand you anything.

A **status code** is the server's first words: a three-digit number at the top of every response. Its first digit is its family:

- **2xx** (read "two hundreds"): it worked.
- **3xx**: look somewhere else; the thing has moved.
- **4xx**: the request has a problem, such as an address with nothing at it. The client should change something before asking again.
- **5xx**: the server broke while answering. The request may have been fine.

The difference between 4xx and 5xx tells you whose code to look at: a 4xx points at the request, a 5xx at the server.

## The codes you'll send most

Read each code as a sentence:

| Code | Read it as | When the garden's server sends it |
|---|---|---|
| `200 OK` | Here's what you asked for | `GET /plots/7` and plot 7 is there |
| `201 Created` | Done: I made it, and here it is | `POST /plots` added a new plot |
| `204 No Content` | Done, and there's nothing to send back | `DELETE /plots/3` removed plot 3 |
| `400 Bad Request` | I can't use what you sent | A sign-up arrives with no gardener's name |
| `401 Unauthorized` | I don't know who you are; sign in first | Someone edits a plot without signing in |
| `403 Forbidden` | I know who you are, and you're not allowed | A neighbor tries to give away someone else's plot |
| `404 Not Found` | There's nothing at that address | `GET /plots/99` |
| `409 Conflict` | That clashes with how things are now | Two neighbors sign up for the same free plot |
| `500 Internal Server Error` | Something broke on my side | A bug in the server's own code |

Node keeps the short name of every code in `STATUS_CODES`, in its `node:http` module:

```javascript
import { STATUS_CODES } from "node:http";

for (const code of [200, 201, 204, 400, 404, 409, 500]) {
  console.log(`${code}: ${STATUS_CODES[code]}`);
}
```

```text
200: OK
201: Created
204: No Content
400: Bad Request
404: Not Found
409: Conflict
500: Internal Server Error
```

## Headers: notes on the package

A parcel carries notes on its label: "fragile", "this side up", who sent it. **Headers** are those notes for a request or response, one `name: value` line each, between the first line and the body. The response above has five:

- `Content-Type: application/json`: the body is JSON, so read it as JSON. Plain text would be `text/plain`, and a web page `text/html`.
- `Date`: when the server answered. Yours shows the moment you sent it.
- `Connection: keep-alive` and `Keep-Alive: timeout=5`: the connection stays open for 5 seconds, ready for the next request.
- `Transfer-Encoding: chunked`: the body comes in pieces, so the server didn't say its length up front.

Content-Type is the one you'll set yourself in every server. Without it, the client has to guess whether the body is JSON, text or a web page.

Requests carry headers too. When a request sends a body, its `Content-Type` says what the body is. In a `.http` file, headers go under the request line, then a blank line, then the body:

```http
POST http://localhost:3100/plots
Content-Type: application/json

{ "size": "medium" }
```

The blank line matters: it's where the headers end and the body starts.

## Do it: choose the garden's status codes

The garden's server is going to answer five situations, and each needs the right status code, so a neighbor's phone app knows what happened.

**Your job:** write `statusFor(situation)`, which returns the status code, as a number, for one of these five situations:

- `"found"`: a neighbor asks for plot 7, and it's there.
- `"created"`: the garden adds a new plot, and the server saved it.
- `"bad input"`: a sign-up arrives with no gardener's name.
- `"not found"`: a neighbor asks for plot 99, which doesn't exist.
- `"crash"`: the server's own code breaks while it's answering.

Worked through: `statusFor("not found")` is about an address with nothing at it. That's a problem with the request, so it's a 4xx code, and the one that says "there's nothing at that address" is 404. It returns `404`.

Practiced live on the lesson page: write the JavaScript right there and check it.

## Check yourself

2 questions to try on the lesson page.

## So what

The status code is the one part of a response every client reads first, so sending the right one is how your server tells apps the truth about what happened.

## Key terms

- [Status Code](https://www.ask-codey.com/glossary/status-code)
- [Header](https://www.ask-codey.com/glossary/header-web-development)
- [Content-Type](https://www.ask-codey.com/glossary/content-type)
- [404 Not Found](https://www.ask-codey.com/glossary/404-not-found)
- [201 Created](https://www.ask-codey.com/glossary/201-created)
- [400 Bad Request](https://www.ask-codey.com/glossary/400-bad-request)

## Related

- [HTTP in Plain Words: Requests and Responses](https://www.ask-codey.com/lessons/node-http-requests-and-responses)
- [A Web Server with Node's http Module](https://www.ask-codey.com/lessons/node-http-first-server)

Source: https://www.ask-codey.com/lessons/node-http-status-codes-and-headers
