Computing Foundations › How the Internet Works › Day 18

Day 18: HTTP: Requests, Responses, and Methods

Day 18 of 365 — HTTP: Requests, Responses, and Methods

After this lesson you will be able to read a raw HTTP request and response, choose the right method, and interpret status codes — so that when a call to a hosted model fails, you can tell a 401 from a 429 from a 500 and know exactly what to do.

Course
Computing Foundations
Category
How the Internet Works
Reading time
≈ 40 min
Practical time
≈ 30 min
Lesson duration
1h 10m
Last verified
2026-07-12

Hands-on lab for this lesson

Lab files on GitHub: https://github.com/ai-roadmap-365/ai-roadmap-365.github.io/tree/main/labs/sections/computing-foundations/day-018-http-requests-responses-and-methods

  1. Get the hands-on files. Clone the labs repository once (you can reuse this clone for every lesson). This works on macOS, Linux, and Windows (PowerShell or WSL):
    git clone https://github.com/ai-roadmap-365/ai-roadmap-365.github.io.git
    cd ai-roadmap-365.github.io
  2. Open this lesson's lab. Move into the directory for this specific day. Every lab lives at the same predictable path — section / subsection / week / day:
    cd labs/sections/computing-foundations/day-018-http-requests-responses-and-methods
  3. Read the lab guide. Open `README.md` in that directory. It lists the exact commands, what each does, the expected output, and how to check your work — read it before running anything.
  4. Run it and check your work. Follow the README's "How to run" section: run the example first to see the finished result, then complete the numbered exercises in `starter/`, then run the tests. The tests pass (exit 0) only when your work is correct.
    bash tests/run_tests.sh   # or the test command named in the lab README

You can also open the lab as a local page (works offline, shows the file tree and expected output).

Learning objectives

By the end of this lesson you will be able to:

Prerequisites

Why this matters


The idea in plain language

Diagram: the four parts shared by every HTTP request and response


Historical background


What it is — and what it is not

It is:

It is not:


Why it was created and what problems it solves


How it works

The request message

POST /v1/messages HTTP/1.1
Host: api.example
Content-Type: application/json
Authorization: Bearer sk-secret-token
Content-Length: 27

{"prompt":"Hello, server"}

The response message

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 41
Cache-Control: no-store

{"reply":"Hello to you too, client!"}

Architecture diagram: an HTTP request message beside an HTTP response message, each labelled part by part

Methods: the verbs

Diagram: safe methods compared with idempotent methods

MethodIntentBody?Safe?Idempotent?
GETRead; change nothingNoYesYes
POSTSend data to be processed; often createsYesNoNo
PUTReplace the resource entirelyYesNoYes
PATCHApply a partial updateYesNoNo
DELETERemove the resourceRarelyNoYes

Status codes: the verdict

Diagram: what each HTTP status class means for what you do next

ClassMeaningYou should
1xxReceived, continueIgnore in everyday work
2xxSuccessRead the body
3xxIt lives elsewhereFollow Location
4xxYour request was wrongFix it — retrying unchanged is pointless
5xxThe server failedRetry with backoff; not your fault

Flow diagram: the five HTTP status-code classes and what each one signals

Headers that matter

Cookies and statelessness

Diagram: how a session cookie is issued and returned

The versions

Diagram: HTTP/1.1 sequential requests compared with HTTP/2 multiplexing


An everyday analogy

A formal letter and its reply.

The postHTTP
”Please send me…”The method
The address on the envelopeThe path
The letterheadThe headers
The blank line before the textThe header/body boundary
The enclosureThe body
”Approved” or “Rejected: incomplete”The status code
A reference number to quote next timeA cookie

Examples in practice

Diagram: 4xx client errors compared with 5xx server errors


Implications: security, privacy, performance, scalability, and cost

Diagram: a credential in a URL compared with one in a header


Alternatives: free, open source, and commercial

ToolTypeWhat it gives you
curlFree, built inAny request you can describe, and -v shows both messages
httpieFree, open sourceThe same, with far friendlier syntax and colour
Browser dev toolsBundled, freeEvery request a page made, with timings
Postman / InsomniaFree tiersSaved collections, environments, sharing
requests / httpxFree, open sourceHTTP from Python; httpx also does async
mitmproxyFree, open sourceWatching and rewriting traffic you control

Diagram: a 401 unauthorized response compared with a 403 forbidden response

TermWhat it actually is
MethodThe verb declaring your intent
PathWhich resource on the server
HeaderMetadata as a Name: value line
BodyThe payload, after the blank line
Status codeThe three-digit verdict
SafeReads only; changes nothing
IdempotentTwice has the same effect as once

When to use it — and when not to

Diagram: which HTTP status codes are worth retrying


The AI connection

Diagram: a buffered response compared with a streamed one


Knowledge check

quiz:
  question: "A client automatically retries any request that times out. Against a model API, this occasionally produces duplicate charges even when the original request succeeded. What property explains it?"
  options:
    - "POST is not idempotent, so a request that succeeded but whose response was lost is performed again"
    - "The API returns 5xx errors for successful requests that exceed the timeout"
    - "Bearer tokens expire mid-request, causing the server to process the call twice"
    - "HTTP is stateless, so the server cannot tell two different clients apart"
  answer_index: 0
  explanation: "A timeout tells you the response did not arrive, not that the work did not happen. Because POST is not idempotent, repeating it does the work a second time — which is why APIs offer idempotency keys for exactly this case."

Hands-on exercise

Read both messages raw, then provoke each status class deliberately. Worked through fully in the Day 18 lab directory.

What you are doingCommand
See the whole exchangecurl -v https://example.com
Headers onlycurl -I https://example.com
Send JSON with a methodcurl -X POST -H 'Content-Type: application/json' -d '{"a":1}' URL
Provoke a 404curl -i https://example.com/nope
Provoke a 401Call an authenticated endpoint with no token
Follow a redirectcurl -iL http://example.com
Time itcurl -w '%{http_code} %{time_total}\n' -o /dev/null -s URL

Expected output

> GET / HTTP/1.1
> Host: example.com
>
< HTTP/1.1 200 OK
< Content-Type: text/html; charset=UTF-8
< Content-Length: 1256

Validate your work

Troubleshooting

Common mistakes


Practice assignment


Extension challenge


Quiz

Q1. What are the four parts of an HTTP request, in order?

  1. Status line, headers, a blank line, then the body
  2. URL, port, protocol, and payload
  3. Method, cookie, encryption, body
  4. Request line, headers, blank line, optional body
Show answer

Answer: D. Request line, headers, blank line, optional body

A request begins with a request line naming the method, path, and HTTP version, followed by headers, a mandatory blank line that marks the end of the headers, and an optional body. A response has the parallel shape but opens with a status line instead of a request line.

Q2. A method is called "idempotent" when:

  1. Repeating it has the same effect as doing it once
  2. It is encrypted while on the wire
  3. It never sends a request body
  4. It only reads data and never changes anything
Show answer

Answer: A. Repeating it has the same effect as doing it once

Idempotent means repeating the request produces the same end state as sending it once — true of GET, PUT, and DELETE. The property that a method only reads and changes nothing is "safe," which is a different (though related) idea; POST is neither safe nor idempotent.

Q3. Your script POSTs a model prompt, the request times out, and it retries automatically. Why is this riskier for POST than for GET?

  1. POST is slower than GET, so retries waste more time
  2. POST is not idempotent, so a retry may create a second resource
  3. GET cannot be retried at all
  4. POST responses are always cached, so the retry returns stale data
Show answer

Answer: B. POST is not idempotent, so a retry may create a second resource

GET is idempotent, so repeating it is harmless. POST is not idempotent: if the first request actually completed but the response was lost, the retry can create a duplicate — for a paid model call, that can mean paying twice. This is why retry logic on POST needs care.

Q4. You call a hosted model API and get a 401 status code. What does it mean and what should you do?

  1. The server crashed; retry the same request in a few seconds
  2. The resource was not found; check the path
  3. Authentication failed; retrying unchanged will not help
  4. You are being rate-limited; slow down your requests
Show answer

Answer: C. Authentication failed; retrying unchanged will not help

401 Unauthorized is a 4xx client error meaning the request lacked valid authentication. It is your request that is wrong, so retrying the identical request forever cannot succeed; you must fix the Authorization header. (429 is rate limiting, 404 is not found, 5xx is a server fault.)

Q5. What distinguishes a 4xx status code from a 5xx status code?

  1. 4xx codes are encrypted; 5xx codes are not
  2. 4xx means success; 5xx means redirection
  3. 4xx is for GET requests; 5xx is for POST requests
  4. 4xx means the request was wrong; 5xx means the server failed
Show answer

Answer: D. 4xx means the request was wrong; 5xx means the server failed

4xx blames the client — a bad path, bad credentials, or malformed body — so you must fix the request before retrying. 5xx blames the server, so a later retry (ideally with backoff) is a reasonable response. Getting this distinction right is the core of debugging failed API calls.

Q6. Which header carries the credential that decides between a 200 and a 401 for a hosted-model call?

  1. Authorization
  2. User-Agent
  3. Content-Type
  4. Cache-Control
Show answer

Answer: A. Authorization

The Authorization header carries your credential, most often as "Bearer <token>". If it is missing or invalid the server answers 401; if it is valid the request can proceed. Content-Type describes the body format, User-Agent identifies the client, and Cache-Control governs caching.

Q7. HTTP is described as "stateless." What does that mean, and how do sites keep you logged in anyway?

  1. The server stores no data at all; login is impossible over HTTP
  2. Each request is self-contained; identity is re-sent by cookie or token
  3. The connection is dropped after every byte, so nothing can persist
  4. State is stored in the URL, which is why URLs are so long
Show answer

Answer: B. Each request is self-contained; identity is re-sent by cookie or token

Statelessness means every request stands alone and the server keeps no memory of you between requests. A session is faked on top: the server sends a Set-Cookie with a session ID, and your client returns it in a Cookie header on every later request, so each self-contained request still lets the server look you up.

Q8. What is the difference between HTTP/1.1, HTTP/2, and HTTP/3?

  1. They use completely different methods and status codes
  2. Only HTTP/3 supports POST; the others are read-only
  3. The wire format differs, but methods and status codes are the same
  4. HTTP/2 and HTTP/3 remove the need for the Authorization header
Show answer

Answer: C. The wire format differs, but methods and status codes are the same

The versions change transport and efficiency — HTTP/2 uses a compact binary form and multiplexes many requests over one connection, and HTTP/3 swaps in the QUIC transport — but the request/response model, methods, headers, and status codes stay identical. Your tools negotiate the version automatically.

Glossary

HTTP
The HyperText Transfer Protocol: the set of rules a client and server follow to exchange request and response messages over a network. It is the protocol every web page load and every hosted-model API call speaks.
request
The message a client sends to a server, made of a request line (method, path, version), headers, a blank line, and an optional body.
response
The message a server sends back, made of a status line (version, status code, reason), headers, a blank line, and a body.
method
The verb of a request that declares intent — GET to read, POST to send data for processing, PUT to replace, PATCH to partially update, DELETE to remove.
status code
A three-digit number in the response that summarizes the outcome; its first digit sorts it into a class (1xx informational, 2xx success, 3xx redirection, 4xx client error, 5xx server error).
header
A Name: value line carrying metadata about a request or response, such as Content-Type or Authorization; headers come before the blank line that precedes the body.
body
The optional payload of a message — the JSON prompt you send in a request, or the HTML or JSON data returned in a response.
GET
The method for reading a resource without changing anything; it is both safe (read-only) and idempotent (repeating it has the same effect as doing it once).
POST
The method for sending a body to be processed, often creating a resource; it is neither safe nor idempotent, so repeating it may duplicate a side effect. Every hosted-model call is a POST.
idempotent
A property of a method meaning that making the same request many times has the same effect as making it once; GET, PUT, and DELETE are idempotent, POST is not.
stateless
The property that each HTTP request is self-contained and the server keeps no memory of a client between requests, which is what lets many identical servers share the load.
Content-Type
A header declaring the format of the body, such as application/json or text/html, so the receiver parses the bytes correctly.
Authorization
A header carrying the credential that proves who the client is, most often as "Bearer <token>"; it is the header that decides between a 200 and a 401.
cookie
A small piece of data the server sends with Set-Cookie and the client returns in a Cookie header on later requests, used to fake a continuous session on top of stateless HTTP.

Sources and further reading


Kept in this browser, no account needed. Your progress page turns the whole record into one link you can bookmark or open on another device.