Sub topic 5
HTTP requests and responses
This chapter assumes you can use a browser and a terminal, have seen basic HTML, and know that a URL names a resource on a server. It covers the shape of an HTTP message and the errors people hit most often when they write one by hand.
One request, one response
HTTP is a text-based protocol. The client sends a request, and the server sends back one response. Each message has three parts: a start line, headers, and an optional body. A blank line separates the headers from the body.
The request
A minimal HTTP/1.1 request looks like this:
GET /index.html HTTP/1.1
Host: example.com
Accept: text/html
- The request line holds the method (
GET), the path (/index.html), and the protocol version. Hostis the only header HTTP/1.1 requires. One server can host many sites, and this header says which one you want.- Each line ends with CRLF (
\r\n). The blank line after the last header marks the end of the headers. A request with no body ends there.
The response
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 14
<h1>Hello</h1>- The status line holds the version, a numeric status code, and a short reason phrase.
Content-Typetells the client how to interpret the body.Content-Lengthis the body size in bytes. The string<h1>Hello</h1>is 14 bytes.
Watching a real exchange
The -v flag makes curl print both messages. Lines starting with > are the request. Lines starting with < are the response.
curl -v https://example.com/A browser sends the same kind of messages, with more headers.
A server that reads the request
A server reads the method and path from each request and writes back a status, headers, and a body.
import { createServer } from "node:http";
const server = createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" });
res.end(`You requested ${req.method} ${req.url}\n`);
});
server.listen(3000);Run this and request http://localhost:3000/hello with curl. You will get You requested GET /hello.
Sending a request from JavaScript
The fetch API exposes the same parts of the response.
const response = await fetch("https://example.com/");
console.log(response.status);
console.log(response.headers.get("content-type"));
const html = await response.text();Common errors
| Mistake | Symptom | Fix |
|---|---|---|
| Missing `Host` header in HTTP/1.1 | `400 Bad Request` | Always send `Host` |
| Wrong `Content-Length` | Truncated body, or the connection hangs waiting for more bytes | Count bytes, not characters |
| Missing blank line after headers | The server waits for the rest of the headers | End headers with CRLF CRLF |
| Wrong `Content-Type` | Browser shows raw text or downloads the file | Match the type to the body |
Exercise
A response says Content-Length: 14, but the body you receive is <h1>Hello</h1> followed by more text. How many bytes of the body should the client treat as part of this response, and why?
Show answer
14 bytes. Content-Length tells the client where the body ends, so <h1>Hello</h1> is the whole body. Any extra bytes are not part of this response, and a well-behaved client will not treat them as such.
Exercise
You send GET / HTTP/1.1 with no other headers and get a 400 response. What is the likely cause, and how do you fix it?
Show answer
The required Host header is missing. Add Host: example.com (or the real host name) to the request.