# Request headers

> The extra information a client sends along with a request.

Source: https://aminbytes.dev/bytes/http/requests/request-headers/


After the first line come **headers**: `Name: value` pairs, one per line, ending with an empty line.

```http {hl_lines="2-5"}
GET /api/profile HTTP/1.1
Host: api.example.com
Accept: application/json
Accept-Language: nl, en;q=0.8
Authorization: Bearer eyJhbGciOi...

```

Header names are case-insensitive: `Accept` and `accept` are the same.

## Headers you'll see all the time

| Header            | Example                  | Tells the server… |
| ----------------- | ------------------------ | ----------------- |
| `Host`            | `api.example.com`        | which website you want (one server can host many). Required in HTTP/1.1 |
| `User-Agent`      | `curl/8.7.1`             | what software is asking |
| `Accept`          | `application/json`       | what formats you can handle |
| `Accept-Language` | `nl, en;q=0.8`           | preferred languages (`q` = preference from 0 to 1) |
| `Content-Type`    | `application/json`       | the format of the body you're sending |
| `Authorization`   | `Bearer <token>`         | who you are |
| `Cookie`          | `session=abc123`         | cookies the server set earlier |

## Cookies: how "stateless" HTTP remembers you

Remember that HTTP is [stateless](../../basics/what-is-http/#stateless). Cookies fix that:

1. You log in. The server answers with a `Set-Cookie` header:
   ```http
   HTTP/1.1 200 OK
   Set-Cookie: session=abc123; HttpOnly; Secure; SameSite=Lax
   ```
2. From now on, the browser adds that cookie to every request to the same site:
   ```http
   GET /account HTTP/1.1
   Host: shop.example.com
   Cookie: session=abc123
   ```
3. The server looks up `abc123` and knows it's you.

> [!CAUTION]
> Anyone who gets that cookie value *is* you as far as the server knows. `HttpOnly` hides it from JavaScript and `Secure` sends it only over HTTPS. Use both for session cookies.

## Sending headers yourself

```bash
curl -H "Accept: application/json" -H "Accept-Language: nl" https://httpbin.org/headers
```

`httpbin.org/headers` echoes back the headers it received, so you can see exactly what was sent:

```json
{
  "headers": {
    "Accept": "application/json",
    "Accept-Language": "nl",
    "Host": "httpbin.org",
    "User-Agent": "curl/8.7.1"
  }
}
```


## Byte check

1. Which header tells the server the format of the body **you're sending**?
   - `Accept`
   - **`Content-Type`** (correct)
   - `Host`
   `Accept` says what you'd like back; `Content-Type` describes what you send.

2. After you log in, how does the server recognize you on the next request?
   - It remembers your IP address
   - **The browser sends the cookie it got from `Set-Cookie` with every request** (correct)
   - The connection stays logged in
   HTTP itself forgets you. The cookie is what reminds the server each time.

3. Which cookie flag hides a session cookie from JavaScript?
   - `Secure`
   - **`HttpOnly`** (correct)
   - `SameSite`
   `Secure` sends it over HTTPS only, `SameSite` limits cross-site sending. Use `HttpOnly` and `Secure` together for sessions.


