The web and SQL databases · Part 20 of 36
HTTP: how browsers and servers talk
Intermediate 16 min
URLs, requests and responses, methods and status codes, headers, cookies, HTTPS and fetch().
What you will learn
- You can read a URL and an HTTP request/response.
- You can choose the right method and status code for an API.
- You can call an API with fetch() and handle errors.
Before this: HTML and CSS: structure and style
From URL to page
When you open https://skillgrove.usefulweb.site/learn?tier=basic, the browser:
- splits the URL into scheme (
https), host (skillgrove.usefulweb.site), path (/learn) and query (tier=basic); - looks up the host's IP address via DNS;
- opens an encrypted connection (TLS);
- sends an HTTP request and receives a response;
- parses the HTML and requests the CSS, scripts and images it references.

Requests and responses
HTTP is a text-based request–response protocol (its meaning is defined in RFC 9110; HTTP/2 and HTTP/3 change how bytes travel, not what they mean):
GET /learn?tier=basic HTTP/1.1
Host: skillgrove.usefulweb.site
Accept: text/html
Accept-Language: en
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=300
<!doctype html>…
A request has a method, a target, headers and optionally a body. A response has a status code, headers and a body.
Methods
| Method | Meaning | Safe? | Idempotent? |
|---|---|---|---|
| GET | read a resource | yes | yes |
| POST | create / process data | no | no |
| PUT | replace a resource | no | yes |
| PATCH | change part of a resource | no | no |
| DELETE | remove a resource | no | yes |
Safe means it doesn't change anything on the server; idempotent means sending it twice has the same effect as once — important when a network error makes you retry.
Status codes
- 2xx success: 200 OK, 201 Created, 204 No Content.
- 3xx redirection: 301 Moved Permanently, 304 Not Modified (use your cached copy).
- 4xx client error: 400 Bad Request, 401 Unauthorized (not logged in), 403 Forbidden (logged in, not allowed), 404 Not Found, 429 Too Many Requests.
- 5xx server error: 500 Internal Server Error, 503 Service Unavailable.
Headers, cookies, caching
Headers carry metadata: Content-Type says what the body is, Cache-Control how long it may be cached, Set-Cookie asks the browser to store a cookie that it sends back on later requests (that is how logins persist). Security headers such as Content-Security-Policy and Strict-Transport-Security tell the browser to block whole classes of attacks.
HTTP itself is stateless: each request stands alone. Sessions, carts and logins are built on top with cookies or tokens.
HTTPS
HTTPS is HTTP inside TLS: it encrypts the traffic and proves the server's identity with a certificate. It is the default today; browsers mark plain HTTP pages as "not secure". HTTP/3 runs over QUIC (on UDP) and is used for a large share of traffic to big sites.
Calling an API with fetch()
const res = await fetch("https://api.example.org/v1/beds", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "north", areaM2: 3.6 }),
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();
Note: fetch only rejects on network failures; a 404 or 500 still "succeeds", so always check res.ok. In Python, the standard library has urllib.request; the third-party requests and httpx packages are more convenient.
Key points
- A URL has scheme, host, path and query; DNS and TLS come before the HTTP request.
- Requests: method, target, headers, body. Responses: status code, headers, body.
- GET is safe and idempotent; POST is neither; PUT and DELETE are idempotent.
- Status classes: 2xx success, 3xx redirect, 4xx client error, 5xx server error.
- HTTP is stateless; cookies/tokens add sessions; HTTPS encrypts; check
res.okwith fetch.
Flashcards
- Question: What is the difference between 401 and 403?
- Answer: 401: not authenticated; 403: authenticated but not allowed.
- Question: What does idempotent mean?
- Answer: Doing it twice has the same effect as doing it once.
- Question: Does fetch() reject on a 404 response?
- Answer: No — it resolves; check res.ok or res.status.
- Question: Which header tells the browser what kind of data the body is?
- Answer: Content-Type.
Check yourself
Think about your answer, then open it. In the app the quiz is interactive, earns points and brings back what you missed.
Match status code and meaning.
Show the answer
201 = Created; 304 = Not Modified; 404 = Not Found; 503 = Service Unavailable
See RFC 9110, section 15.
Source: RFC 9110
Which HTTP methods are idempotent?
- GET
- PUT
- DELETE
- POST
Show the answer
GET; PUT; DELETE
GET, PUT and DELETE have the same effect when repeated; POST may create a new resource each time.
Source: RFC 9110, 9.2.2
A user is logged in but tries to open another user’s invoice. The best status code is…
- 403 Forbidden (or 404 to hide that it exists)
- 401 Unauthorized
- 200 OK
- 500 Internal Server Error
Show the answer
403 Forbidden (or 404 to hide that it exists)
The user is authenticated but not authorised; 404 is also used to avoid revealing existence.
Source: RFC 9110, 15.5.4
In the URL https://example.org/learn?tier=basic the part tier=basic is called the ___ string.
Show the answer
query
Everything after ? is the query.
Source: RFC 3986 / MDN, URL
Sources
Facts checked on 2026-11-30. Skillgrove lessons are free learning material — no certificate and no accreditation.