How the Web Talks: HTTP Requests and Responses
Take apart a real HTTP exchange: the URL, method, headers and body of a request, and the status code, headers and body of the response that comes back.
Welcome back!
Step 1 of 16Learn
Anatomy of a URL
Every web API call starts with a URL (uniform resource locator). Take this one apart:
https://api.example.com:443/v1/books/42/reviews?sort=newest&limit=5
httpsis the scheme: the protocol to use. HTTPS is HTTP encrypted with TLS, and real APIs should always use it, so nobody between client and server can read or alter the data.api.example.comis the host: which server to contact. Many companies put their API on its own subdomain.443is the port. It's the default for HTTPS (80 for plain HTTP), so it's almost always left out./v1/books/42/reviewsis the path: which resource you want on that server. Here: the reviews of book 42, in version 1 of the API.?sort=newest&limit=5is the query string: optional parameters askey=valuepairs, joined with&. They usually filter, sort or page through results.
A URL can also end with a fragment such as #section-2, but fragments are handled by the browser and never sent to the server, so APIs don't use them.
When you read API documentation, you'll usually see a base URL such as https://api.example.com/v1 and then a list of paths, called endpoints, relative to it: GET /books, GET /books/{id}. Curly braces mark placeholders you fill in.
Step 2 of 16Practice · Match the pairs
Match each part of https://api.shop.com/v2/orders/981?expand=items to its name.
Tap an answer, then tap the place where it belongs (or drag it there).
httpsapi.shop.com/v2/orders/981expand=items
Step 3 of 16Diagram
The parts of an API URL
Example: https://api.example.com:443/v1/books/42/reviews?sort=newest&limit=5
Tap a shape with to see more.
- Full URL
- Scheme: https
The protocol. For APIs it should always be https.
- Host: api.example.com
The server name, turned into an IP address by DNS.
- Port: 443
Usually left out, because 443 is the default for https.
- Path: /v1/books/42/reviews
Which resource you want, often including a version.
- Query: sort=newest&limit=5
Optional key=value options after the
?, joined by&.
Step 4 of 16Learn
What a request contains
An HTTP request is plain structured text. A request to create a review might look like this on the wire:
POST /v1/books/42/reviews HTTP/1.1
Host: api.example.com
Content-Type: application/json
Accept: application/json
Authorization: Bearer abc123token
{"rating": 5, "text": "Couldn't put it down."}It has four parts:
- Method:
POST, the action to perform. The main methods are GET (read), POST (create), PUT and PATCH (update) and DELETE (remove). You'll study them in detail later. - Path (plus query string): which resource the action applies to.
- Headers:
Name: valuelines with metadata about the request.Content-Typesays what format the body is in;Acceptsays what format the client wants back;Authorizationproves who is calling. - Body: optional data sent with the request, here a JSON object with the review. GET requests normally have no body; their inputs go in the path and query string.
A blank line separates the headers from the body. Header names are case-insensitive, so content-type and Content-Type mean the same thing.
Newer versions, HTTP/2 and HTTP/3, encode the same information in a compact binary form for speed, but the model is identical: method, path, headers, optional body.
Step 5 of 16Practice · Sort into groups
Where does each piece of information belong in an HTTP request?
Tap an item, then tap its group (or drag it there).
Step 6 of 16Diagram
Every HTTP message has the same four layers
Tap a shape with to see more.
- Start line
Request: method, path, version (
POST /v1/books HTTP/1.1). Response: version and status (HTTP/1.1 201 Created).- Headers
One
Name: valueper line: Host, Content-Type, Authorization and so on.- Blank line
Marks where the headers end.
- Body (optional)
The data itself, usually JSON. GET requests normally have none.
Step 7 of 16Learn
What a response contains
The server answers with a response that has a matching shape:
HTTP/1.1 201 Created
Content-Type: application/json
Location: /v1/books/42/reviews/7781
Date: Tue, 03 Mar 2026 10:15:02 GMT
{"id": 7781, "rating": 5, "text": "Couldn't put it down.", "createdAt": "2026-03-03T10:15:02Z"}- Status line: the protocol version and a status code with a short reason phrase.
201 Createdmeans the review was created. The three-digit code is what programs check; the phrase is for humans. - Headers: metadata about the response.
Content-Typesays the body is JSON;Locationgives the address of the newly created review; other common headers control caching (Cache-Control) or report rate limits. - Body: the data, usually JSON. Here the server returns the saved review, including fields the server added, the
idandcreatedAt.
Status codes are grouped by their first digit:
- 2xx success (200 OK, 201 Created, 204 No Content)
- 3xx redirection (the resource is elsewhere)
- 4xx client error (400 Bad Request, 401 Unauthorized, 404 Not Found)
- 5xx server error (500 Internal Server Error, 503 Service Unavailable)
You'll learn to handle each family properly in the REST module. For now, remember the most important habit: always check the status code before trusting the body.
Step 8 of 16Practice · Work it out
A response arrives with the status line HTTP/1.1 404 Not Found. What is the first digit of the status code, which tells a program the general family of the result?
Step 9 of 16Practice · Spot the mistake
This raw request is meant to send a new user as JSON. Click the lines with mistakes.
Step 10 of 16Practice · Code challenge
Write a function statusFamily(code) that returns the family of an HTTP status code as a string: "success" for 200–299, "redirect" for 300–399, "client error" for 400–499, "server error" for 500–599, and "unknown" for anything else.
statusFamily(201)statusFamily(304)statusFamily(404)statusFamily(503)statusFamily(99)statusFamily(299)
Step 11 of 16Note
Step 12 of 16Learn
HTTPS and why it is not optional
Plain HTTP sends everything as readable text. Anyone positioned between the client and the server, such as someone on the same public Wi-Fi, a compromised router or a malicious proxy, can read the request and response or even change them in transit. For an API, that would expose API keys, tokens, personal data and payment details.
HTTPS wraps HTTP in TLS (Transport Layer Security), which provides three guarantees:
- Encryption: eavesdroppers see only scrambled bytes.
- Integrity: any tampering with the data in transit is detected.
- Authentication: the server proves its identity with a certificate issued by a trusted authority, so the client knows it's really talking to
api.example.comand not an impostor.
The HTTP messages inside are exactly the same as you've seen; TLS just protects the connection they travel over. Certificates are free and automated today, so there's no reason for any API to accept plain HTTP. Many APIs refuse it outright or redirect it, and some security guides recommend rejecting plain HTTP API calls rather than redirecting them, because the first unencrypted request may already have leaked a key.
Step 13 of 16Practice · Select all that apply
Which protections does HTTPS (HTTP over TLS) provide? Select all that apply.
Select all that apply.
Step 14 of 16Compare
HTTP vs HTTPS on public Wi-Fi
| Aspect | Plain HTTP | HTTPS (HTTP over TLS) |
|---|---|---|
| Can others read the token? | Yes, it travels as plain text | No, it is encrypted |
| Can the response be altered? | Yes, silently | No, tampering is detected |
| Do you know who answered? | No | Yes, the certificate proves the server |
| Default port | 80 | 443 |
Step 15 of 16Tap to reveal
Common request and response headers
Content-Type
Says what format the body is in, for example
application/json. Sent on requests with a body and on most responses.Accept
Sent by the client to say which formats it can handle, for example
application/json. The server may reply 406 if it can't provide one.Authorization
Carries credentials such as
Bearer <token>so the server knows who is calling.Cache-Control
Tells clients and proxies whether and for how long a response may be reused, for example
max-age=60.Location
Sent with 201 Created or redirects to give the URL of the new or moved resource.
Step 16 of 16Practice · Choose one
A client sends GET /v1/products?category=books and receives 200 OK with a JSON body. Where did the client put its input, and why?
Lesson complete!