Sub topic 8
Headers and content types
This chapter assumes you can read a basic HTTP request and response, including the request line, status line, and blank line that ends the headers. It covers how headers describe a message and how Content-Type tells the receiver how to interpret the body.
What headers are
A header is a Name: value line placed after the first line of a request or response. Headers carry metadata: who is asking, what formats are acceptable, how large the body is, and how to treat it. Header names are case-insensitive. Values are not always.
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 14
<h1>Hello</h1>Content-Length: 14 is the size of the body in bytes. <h1>Hello</h1> is exactly 14 bytes, so the client knows when the body ends.
Content types
Content-Type names the body's media type, written as type/subtype, with optional parameters after a semicolon. The browser uses it to decide what to do with the bytes.
| Content-Type | Meaning | Typical handling |
|---|---|---|
| `text/html; charset=utf-8` | HTML document | Parsed and rendered |
| `text/css` | Stylesheet | Applied to the page |
| `application/json` | JSON data | Read by scripts |
| `image/png` | PNG image | Displayed |
| `application/octet-stream` | Unknown binary | Usually downloaded |
The charset parameter says how to decode text bytes into characters. A wrong or missing charset is a common cause of garbled characters such as é in place of é.
A request with a body also sends Content-Type. A form submission might send application/x-www-form-urlencoded, and an API call usually sends application/json. If the header does not match the actual body, many servers reject the request or parse it incorrectly.
Content negotiation
The client can state what it accepts, and the server picks a representation.
Acceptlists preferred media types, such astext/htmlorapplication/json.Accept-Languagelists preferred languages.Accept-Encodinglists compression schemes, such asgzip.
If the server compresses the body, it says so in Content-Encoding: gzip. Content-Length then counts the compressed bytes, not the original text.
Reading headers in code
This Node.js server returns the request headers it received. Node lowercases header names, so you look them up in lowercase.
import { createServer } from "node:http";
const server = createServer((req, res) => {
const accept = req.headers["accept"] ?? "none";
const agent = req.headers["user-agent"] ?? "unknown";
const body = `Accept: ${accept}\nUser-Agent: ${agent}\n`;
res.writeHead(200, {
"Content-Type": "text/plain; charset=utf-8",
"Content-Length": Buffer.byteLength(body),
});
res.end(body);
});
server.listen(3000);Use Buffer.byteLength, not body.length. The string length counts characters, and the header needs bytes. They differ for any non-ASCII text.
To see the headers from the command line, run this while the server is running:
curl -v -H "Accept: text/plain" http://localhost:3000/Common errors
- Wrong
Content-Typeon a response. A server sending JSON astext/htmlmakesfetchstill work, but browsers may render it as a page, and some tools refuse it. - Mismatched
Content-Length. A value that is too small truncates the body. A value that is too large makes the client wait for bytes that never arrive. - Missing charset. The browser guesses the encoding and may guess wrong.
Exercise
A server returns a JSON body with the header Content-Type: text/plain. The body is 20 bytes, and the response has Content-Length: 20. What can go wrong, and which header should you change?
Show answer
The length is fine, but the type is wrong. Clients that rely on the type may treat the body as plain text instead of JSON. Change Content-Type to application/json.