15 min read
HTTP request and response: anatomy and examples
In one sentence: An HTTP request carries the path and the query string in its first line, then its headers and, if it sends data, a body after the empty line. Content-Type says what format that body is in and Content-Length how many bytes it takes up; a response is written the same way.
What you will learn
- Split a URL into its parts and know which ones reach the server and which one never leaves the browser.
- Read a request with a body and understand its Content-Type and Content-Length headers.
- Tell a form from JSON, and decide whether some data goes in the query string or in the body.
- Play the server with nc and write by hand a response that curl accepts on its own.
Before you start, read: phase-0/09-from-url-to-page.md phase-1/05-processes-and-signals.md
The problem
Section titled “The problem”Imagine Ana and Luis launch their shop’s new “Buy” button. Luis tries it with a mug: the server gets the order, but answers that it’s empty, with no product and no quantity. Ana swears the button sends both pieces of data. Who’s right? There’s only one way to find out: look at the request exactly as it reaches the server, byte by byte.
In Phase 0 you saw short requests, a line, a few headers and an empty line, that only asked for something. But a purchase, a search or a login send data, and whoever answers has to know where to look for it and how to read it.
In this lesson you’ll play the server by hand, as in Phase 0, but this time you’ll read every header and write a response the client considers finished without you cutting the connection.
The analogy
Section titled “The analogy”The analogy
Think of a package you send by courier. On the outside it has the address and a label with what you need to know before opening it: what it contains (“documents,” “fragile”) and how many pieces there are. Inside the box goes what you’re sending.
An HTTP request is written the same way. The first line and the Host header are the address; the other headers, the label; and the body, what goes in the box. Whoever receives it reads the label before opening: if it says “three pieces” and two have arrived, they wait for the third.
Where the analogy breaks down
- Whoever receives a package can open it and see what’s inside, whatever the label says. A server doesn’t guess: if the label says “form” and there’s something else inside, it reads it wrong.
- HTTP’s label doesn’t count pieces, but exact bytes. One letter too many or too few, an Enter included, and it no longer adds up.
- Many requests are like a letter without a box: the ones that only ask for something carry no body.
How it really works
Section titled “How it really works”The parts of a URL, and which ones travel
Section titled “The parts of a URL, and which ones travel”This is an address in the shop, a URLURL stands for Uniform Resource Locator.The address of something on the web, such as https://shop.example/products?sort=price. It says which protocol to ask for it with (https), which server to ask (shop.example) and which page or file on that server (/products), and it can carry extra data in the query string.Go to definition with all its parts:
https://shop.example/products?category=mugs&sort=price#reviews| Part | In the example | What it’s for | Does it reach the server? |
|---|---|---|---|
| Scheme | https |
The protocol, and with it the default port, 443 | Not as text: it decides how the browser connects |
| Server name | shop.example |
Which server to go to (its IP comes from DNS) | Yes, in the Host header |
| Port | Not there: it’s 443 | Which program on the server answers | Only if it isn’t the default one, inside Host, as in localhost:8080 |
| Path | /products |
Which page or file you want | Yes, in the first line |
| Query string | ?category=mugs&sort=price |
Extra data about what you’re asking for | Yes, in the first line, stuck to the path |
| Fragment | #reviews |
A spot inside the page | No: it stays in the browser |
When you press Enter with that URL, what goes out to the server starts like this:
GET /products?category=mugs&sort=price HTTP/1.1Host: shop.exampleThe browser uses the fragment once it already has the page, to scroll down to the reviews section. The server never sees it: it can’t save it in its LogA text file where a program writes down what happens, one line for each thing. A web server’s has a line for each request it receives, and it’s the first thing you look at when something fails.Go to definition or answer depending on what it says.
The query string: data in the URL itself
Section titled “The query string: data in the URL itself”The Query stringThe part of a URL that comes after “?”: data in name=value pairs separated by “&”, such as q=mug&sort=price. It travels to the server attached to the path, and it’s used to search, filter or choose a page of results.Go to definition starts at the ? and carries name=value pairs separated by &. In ?category=mugs&sort=price there are two: the category and the sort order. What each name means is up to the server; the protocol only says where they go.
A URL can’t carry spaces or letters like the é in café as they are, so they’re encoded. In a query string, a space is written + (or %20), and an é, %C3%A9: its two bytes in hexadecimal, the base 16 from Phase 0, each with a % in front. Searching for “red mug” would give /search?q=red+mug.
Since it goes in the URL, it gets written down in the server’s logs: the access.log from Phase 1 is full of lines like GET /api/products?page=3.
The body: data after the empty line
Section titled “The body: data after the empty line”This is how an order sent as a form reaches the server, with the product and the quantity:
POST /orders HTTP/1.1Host: localhost:8080User-Agent: curl/8.5.0Accept: */*Content-Length: 22Content-Type: application/x-www-form-urlencoded
product=mug&quantity=2Line by line:
| Line | What it is |
|---|---|
POST /orders HTTP/1.1 |
The first line. POST is the HTTP methodHTTP stands for HyperText Transfer Protocol.The word every HTTP request starts with, which says what the client wants to do with what it asks for. GET is reading, POST sending data, PUT replacing, PATCH changing a part and DELETE deleting.Go to definition for sending data (lesson 2) |
Host, User-Agent and Accept |
The usual headers: which server it’s going to, which program sends it and which formats it accepts as a response (*/*, any) |
Content-Length: 22 |
How many bytes the body takes up |
Content-Type: application/x-www-form-urlencoded |
What format the body is written in: here, a form |
| (empty line) | The end of the headers |
product=mug&quantity=2 |
The Body (of a request or response)The data an HTTP message carries after the blank line that ends the headers, such as the fields of a form, some JSON or an image. The Content-Type header says what format it’s in, and Content-Length how many bytes it takes up.Go to definition: the data |
product=mug&quantity=2 has 22 characters, which is what Content-Length announces. The server reads the headers up to the empty line and then reads exactly 22 bytes, without waiting for the connection to close, which in HTTP/1.1 stays open for the next request.
Watch out: it counts bytes, not letters (an é takes two), and the body doesn’t end in a line break, but where the number says. Without Content-Length (and without sending it in chunks), the server understands there’s no body.
Content-Type says what is in the body
Section titled “Content-Type says what is in the body”Content-Type carries a MIME typeMIME stands for Multipurpose Internet Mail Extensions.The label that says what kind of data a message carries, in the form type/subtype: text/html is a web page, application/json is data in JSON and image/png a PNG image. In HTTP it goes in the Content-Type header. It’s also called a media type.Go to definition: two words in the form type/subtype. The ones you’ll see the most:
| MIME type | What’s in the body |
|---|---|
text/html |
A web page |
text/plain |
Plain text |
application/json |
Data in JSON |
application/x-www-form-urlencoded |
The fields of a form |
multipart/form-data |
A form with files, like a photo |
image/png, image/jpeg |
An image |
Sometimes they carry a parameter after them, like text/plain; charset=utf-8, which says which character set the text is written in.
It works in both directions: in a request it says what the client is sending, and in a response, what the server returns. The browser decides what to do with a response by its Content-Type, not by how the URL ends. And it isn’t the same as Accept, what the client prefers to receive: you’ll see it in lesson 4.
A form or JSON: the same data, two formats
Section titled “A form or JSON: the same data, two formats”The same order, written in the two most common ways:
| Form | JSON | |
|---|---|---|
Content-Type |
application/x-www-form-urlencoded |
application/json |
| Body | product=mug&quantity=2 |
{"product":"mug","quantity":2} |
| Who usually sends it | A form on a website, without programming anything | A website’s JavaScript code, or a phone app |
| What it can express | name=value pairs, and everything is text | Numbers, lists and data inside other data |
The form format is the query string’s, with the same encoding: hence its name, urlencoded, “encoded as in a URL.” A search form, which asks instead of sending, puts that text in the URL: that’s where ?q=red+mug comes from.
JSONJSON stands for JavaScript Object Notation.A format for writing data as text, with braces, quotes and colons: {"product":"mug","quantity":2}. People can read it, and so can almost any programming language, and it’s the most common format for the data that travels between a frontend and a backend.Go to definition, the format from the first lesson of the course, tells the number 2 from the text "2" and allows lists, and that’s why APIAPI stands for Application Programming Interface.The way a program offers its services to other programs, that is, what you can ask it for and how it answers. The frontend talks to the backend through an API.Go to definition prefer it.
The server chooses how to read the body by the Content-Type, not by what it looks like. That was the shop’s bug: Ana’s button sent correct JSON, but labeled as text/plain, and the server didn’t read it. The order arrived whole; the label lied.
Query string or body?
Section titled “Query string or body?”Both carry data to the server, but they aren’t for the same thing:
| Query string | Body | |
|---|---|---|
| Where it goes | In the URL, in the first line | After the empty line |
| Where it stays | In the browser’s address bar, the history, the bookmarks and the logs | Only in the request |
| Size | Short: nginx, for example, rejects a first line over 8 KB | It can take up megabytes, like a photo |
| What for | Saying what you want to see: search, filter, paginate | Sending something to be saved or changed: an order, a comment, a password |
A rule that almost never fails: if it makes sense to share it as a link, it goes in the query string; if not, in the body. A password never goes in the URL. With HTTPSHTTPS stands for HyperText Transfer Protocol Secure.HTTP inside a TLS connection, usually on port 443. Nobody along the way can read or change the requests and responses, and the browser uses a certificate to check that the server really belongs to the domain.Go to definition, the path and the query string travel encrypted, as you saw in the TLS lesson, but the history and the logs keep them in plain text.
A response, field by field
Section titled “A response, field by field”This is the response you’ll write yourself in “Try it”:
HTTP/1.1 200 OKContent-Type: application/jsonContent-Length: 13
{"order":17}| Line | What it is |
|---|---|
HTTP/1.1 200 OK |
The status line from Phase 0: version, Status codeA three-digit number at the start of every HTTP response that sums up what happened. 200 means “all good,” 404 “that doesn’t exist” and 400 “your request is malformed.”Go to definition and a phrase for humans (lesson 3) |
Content-Type: application/json |
The body is JSON |
Content-Length: 13 |
It takes up 13 bytes |
| (empty line) | End of the headers |
{"order":17} |
The body: the order number |
Why 13, if {"order":17} has 12 characters? Because you’re going to type it in the terminal and press Enter, and the Enter travels too: it’s one more byte, the line break. With the exact number, the client knows the response is complete and doesn’t wait for more.
Seeing the messages with curl -v and curl -i
Section titled “Seeing the messages with curl -v and curl -i”By default, curl only writes the body of the response. These options show the rest, or send data:
-i(for include) also writes the status line and the headers of the response.-v(for verbose) shows everything: the lines with*arecurl’s notes, the ones with>what it sends and the ones with<what it receives. The body has no mark.-d(for data) sets a body, and with itcurlusesPOSTand the form label.-H(for header) adds or changes a header:-H 'Content-Type: application/json'.--jsonsets a JSON body and, at the same time, theContent-TypeandAcceptheaders.
Try it
Section titled “Try it”The goal: play the shop’s server. You’ll find out which part of a URL really arrives, receive an order in JSON and answer it by hand so well that curl considers the response finished without you cutting the connection.
You need two terminal windows and nothing else. The nc and curl outputs are from an Ubuntu with curl 8.5.0: on your computer you’ll see your version (on a Mac, something like curl/8.7.1) and maybe a different * line.
First, a real response, to see what you’re going to imitate:
1. Read a real response
curl -i https://backenddesdecero.com/robots.txtWhat you will see:
HTTP/2 200date: Mon, 23 Nov 2026 15:28:38 GMTcontent-type: text/plaincontent-length: 80cf-cache-status: HITcache-control: public, max-age=0, must-revalidateetag: "c0ddff9d554300baca1ac0bccc43ef2e"strict-transport-security: max-age=86400referrer-policy: strict-origin-when-cross-originx-content-type-options: nosniff…server: cloudflarecf-ray: a5f13470783cfbb5-MADalt-svc: h3=":443"; ma=86400
User-agent: *Allow: /
Sitemap: https://backenddesdecero.com/sitemap-index.xmlIt’s the file this site leaves for search engines. Look for content-type: text/plain and content-length: 80: the four lines of the body, with their line breaks, add up to those 80 bytes.
HTTP/2 200, without the OK and with the headers in lowercase, is HTTP/2: the same information in another format (lesson 8). If you get HTTP/1.1 200 OK, the rest is the same.
We removed two long Cloudflare headers (…); your date and your cf-ray will be different.
Now, you’re the server. In the first window start nc listening on port 8080, as in Phase 0, and in the second ask for a search, fragment included:
curl 'http://localhost:8080/search?q=red+mug#results'2. Which part of the URL arrives? (first window)
nc -l 8080What you will see:
GET /search?q=red+mug HTTP/1.1Host: localhost:8080User-Agent: curl/8.5.0Accept: */*This shows up in the nc window as soon as you run the curl. Compare it with the URL: the path and the query string arrived in the first line, and the port, in Host. The #results is nowhere to be seen: curl, like the browser, doesn’t send it.
The quotes around the URL are needed: without them, the shell treats the ? and the & as its own symbols. If on Linux nc -l 8080 ends with an error, try nc -l -p 8080.
curl keeps waiting for a response. Press Ctrl + C in the nc window: the connection closes and curl ends with curl: (52) Empty reply from server.
Start nc -l 8080 again in the first window. In the second, send an order in JSON, with -v to see the whole conversation:
curl -v -H 'Content-Type: application/json' -d '{"product":"mug","quantity":2}' http://localhost:8080/orders3. Receive an order (first window)
nc -l 8080What you will see:
POST /orders HTTP/1.1Host: localhost:8080User-Agent: curl/8.5.0Accept: */*Content-Type: application/jsonContent-Length: 30
{"product":"mug","quantity":2}There’s the whole order. Look at the two headers: you set Content-Type: application/json with -H, and curl worked out Content-Length: 30.
The cursor stays stuck at the end of the JSON, without moving to a new line: the body ends at byte 30, not at an Enter. Don’t stop anything: the order is waiting for a response.
Now answer. Click on the nc window and type these lines, pressing Enter at the end of each one. Start typing right away, even if the first line comes out stuck to the JSON: an extra Enter at the start breaks the response.
4. Answer with its size (what you type in the first window)
HTTP/1.1 200 OKContent-Type: application/jsonContent-Length: 13
{"order":17}What you will see:
* Host localhost:8080 was resolved.* IPv6: ::1* IPv4: 127.0.0.1* Trying [::1]:8080...* connect to ::1 port 8080 from ::1 port 37200 failed: Connection refused* Trying 127.0.0.1:8080...* Connected to localhost (127.0.0.1) port 8080> POST /orders HTTP/1.1> Host: localhost:8080> User-Agent: curl/8.5.0> Accept: */*> Content-Type: application/json> Content-Length: 30>< HTTP/1.1 200 OK< Content-Type: application/json< Content-Length: 13<{"order":17}* Connection #0 to host localhost left intactThe output is the second window’s, the curl one: with >, the request; with <, your response; and at the end, the body.
What matters is what you didn’t do: press Ctrl + C. curl received the 13 bytes announced and finished on its own; left intact says the connection was still healthy for another request. When it finishes, curl closes it, and nc ends too.
If curl keeps waiting, the number is bigger than the body or you missed the last Enter. If it writes Received HTTP/0.9 when not allowed, you pressed Enter before the first line.
Challenge: repeat steps 3 and 4, but announce Content-Length: 20 with the same body. What does curl do? And what happens when you stop nc?Show answer
curl writes {"order":17} and keeps waiting: you promised it 20 bytes and only 13 have arrived. For curl, the response isn’t over.
When you press Ctrl + C in the nc window, the connection closes and curl complains:
* transfer closed with 7 bytes remaining to read* Closing connectioncurl: (18) transfer closed with 7 bytes remaining to readThe 7 bytes it was still waiting for are the 20 promised minus the 13 received.
You have already seen it
Section titled “You have already seen it”- The browser’s address bar after searching on Google has something like
?q=and your words joined with+: it’s a form that asks, and that’s why it puts the data in the query string. - Links that jump to a section, like the ones in this page’s outline or Wikipedia’s, end in
#and a name: a fragment the server never gets to see. - When you upload a photo on a website, the form usually sends it in the body with
multipart/form-data. - In DevTools (how to open them), in the Network tab, click on a request: Payload (in Firefox, Request) shows the query string and the body split into fields, and Headers, its
Content-Typeand itsContent-Length.
And if you code:
fetch(url, { method: 'POST', body: JSON.stringify(order) })sendsContent-Type: text/plain;charset=UTF-8: the shop’s bug. Addheaders: { 'Content-Type': 'application/json' }.- With a
FormDataas the body, the browser setsmultipart/form-dataon its own, with the separator between fields. If you write that header yourself, the separator is lost. new URL(location.href)splits out the parts from the table:searchis the query string andhash, the fragment.
Common mistakes
Section titled “Common mistakes”- Typing the URL without quotes in the terminal. zsh, the Mac’s shell, takes the
?as a file name wildcard and answerszsh: no matches found:without running anything. bash takes the&as “send this to the background” and cuts the URL there. Put the URL in single quotes. - Pressing Enter before typing the response in
nc. That Enter travels:curlgets an empty line where it expectedHTTP/1.1 200 OKand answerscurl: (1) Received HTTP/0.9 when not allowed. - Counting the
Content-Lengthwrong. If you announce more bytes than you send, the client waits, as in the challenge. If you announce fewer, it cuts the body: withContent-Length: 9,curlwrites only{"order":. - Labeling the body wrong, like the shop’s JSON with
text/plain, or JSON sent withcurl -dwithout-H, which goes out labeled as a form. The server doesn’t guess the format. - Trusting what the request says. The
Content-Typeand theContent-Lengthare written by the client, and they can lie, as you lied in the challenge. A server checks that the body is what it claims to be before using it; you’ll do that in Phase 3.
Summary
Section titled “Summary”- The path and the query string travel in the first line, and the server name in
Host. The fragment (#…) never leaves the browser. - Data goes in the query string if it says what you want to see, and in the body, after the empty line, if you’re sending something to be saved or changed.
Content-Typesays what format the body is in andContent-Length, exactly how many bytes it takes up. The server reads the body according to both.curl -ishows the response headers,curl -vthe whole conversation andnc -l, the request as it arrives.- In Phase 3, a program of yours will do
nc’s job: read the request, understand its body and write the response with its size.
Did you get it?
Section titled “Did you get it?”To review:
Further reading
Section titled “Further reading”- HTTP messages (MDN): the structure of requests and responses, with more examples.
- What is a URL? (MDN): every part of a URL, with drawings.
- Content-Type (MDN): the header and its most common values.
- Sending form data (MDN): what a form sends and how, with
GETand withPOST.