3. Client and Server
1
The picture to keep in your head2
3.1 What exactly are a client and a server?3
3.2 What's a request and a response?4
3.3 What happens when you type a URL and press Enter?5
3.4 What do GET, POST, PUT, PATCH and DELETE mean?6
3.5 What do status codes tell you?7
3.6 What goes in the headers and what goes in the body?8
3.7 What's the difference between TCP and UDP?9
3.8 How does HTTPS keep the conversation private?10
3.9 Why do keep-alive, HTTP/2 and HTTP/3 make things faster?11
3.10 What does "stateless" mean, and why do we want it?12
3.11 How do cookies, sessions and tokens remember who you are?13
Memory card and where the analogy breaks14
Practice: five levels15
Interview questions16
Solutions17
Sources18
Code lab3. Client and Server
3.6 What goes in the headers and what goes in the body?
3 min read+10 XP
In the restaurant: The order slip has a top part (table number, "allergic to peanuts", "pay by card") and the order itself. The top part tells the kitchen how to handle the order; the order says what you want.
In the system:
- Headers are labels about the message: who it's for, what format, how big, who's asking, how long it can be cached.
- The body is the content itself: the JSON for a new cart item, the HTML of a page, an image.
Headers you'll meet again in this book:
| Header | What it says | Chapter |
|---|---|---|
Host | Which website this is for (one server can host many) | here |
Content-Type | The format of the body, e.g. application/json | here |
Authorization | Who you are, e.g. a token | 3.11, 24 |
Cookie / Set-Cookie | Small values the browser stores and sends back | 3.11 |
Cache-Control | Whether and how long the response can be cached | 13, 26 |
Retry-After | When to try again (with 429 or 503) | 25 |
A GET usually has no body: everything needed is in the path and headers. A POST usually has one.
Memory code: "Headers describe, body delivers."