Notes › Computer Networks
HTTP and HTTPS — How the Web Talks
HTTP (HyperText Transfer Protocol) is the application-layer protocol browsers and apps use to talk to servers. It is request–response and stateless: each request carries everything the server needs; the server remembers nothing between requests (cookies and tokens add state on top).
A request and a response
POST /api/notes HTTP/1.1
Host: codify.example
Content-Type: application/json
Authorization: Bearer eyJhbGci…
Content-Length: 34
{"title":"Graphs","tags":["dsa"]}
HTTP/1.1 201 Created
Content-Type: application/json
Location: /api/notes/42
Cache-Control: no-store
{"id":42,"title":"Graphs"}
Request = method + path + version, headers, blank line, optional body. Response = version + status code + reason, headers, blank line, body.
Methods
| Method | Purpose | Body | Safe | Idempotent |
|---|---|---|---|---|
| GET | read a resource | no | ✅ | ✅ |
| POST | create / perform an action | yes | ❌ | ❌ |
| PUT | replace a resource completely | yes | ❌ | ✅ |
| PATCH | change part of a resource | yes | ❌ | ❌ (usually) |
| DELETE | remove a resource | rarely | ❌ | ✅ |
| HEAD | like GET, headers only | no | ✅ | ✅ |
| OPTIONS | which methods are allowed (CORS preflight) | no | ✅ | ✅ |
- Safe = doesn't change server data.
- Idempotent = doing it 1 time or 10 times has the same effect (safe to retry). Deleting the same note twice still leaves it deleted.
Status codes
| Range | Meaning | Common ones |
|---|---|---|
| 1xx | informational | 101 Switching Protocols (WebSocket upgrade) |
| 2xx | success | 200 OK, 201 Created, 204 No Content |
| 3xx | redirection | 301 Moved Permanently, 302/307 temporary, 304 Not Modified |
| 4xx | client error | 400 Bad Request, 401 Unauthorized (not logged in), 403 Forbidden (no permission), 404 Not Found, 405 Method Not Allowed, 409 Conflict, 422 Unprocessable, 429 Too Many Requests |
| 5xx | server error | 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable, 504 Gateway Timeout |
Important headers
| Header | Direction | Use |
|---|---|---|
Host |
req | which website (many sites share one IP) |
Content-Type |
both | application/json, text/html, multipart/form-data |
Authorization |
req | Bearer <token> |
Cookie / Set-Cookie |
req / res | send / set cookies |
Accept, Accept-Encoding |
req | wanted format, gzip, br compression |
Cache-Control, ETag, Last-Modified |
res | caching |
Location |
res | where to redirect / the new resource |
Access-Control-Allow-Origin |
res | CORS |
User-Agent |
req | browser / client info |
Cookies (state on a stateless protocol)
Set-Cookie: sid=abc123; HttpOnly; Secure; SameSite=Lax; Max-Age=604800; Path=/
The browser sends Cookie: sid=abc123 on later requests to that site. Flags: HttpOnly (JS can't read it), Secure (HTTPS only), SameSite (limits cross-site sending → CSRF protection). See Authentication — JWT, Sessions and Cookies.
Caching
Cache-Control: max-age=3600→ reuse for an hour without asking.Cache-Control: no-store→ never cache (private data).- Conditional request: the browser sends
If-None-Match: "etag-value"; if unchanged the server replies 304 Not Modified with no body — saves bandwidth. - CDNs cache static files close to users (Caching, CDN and Databases at Scale).
CORS (Cross-Origin Resource Sharing)
Browsers block JS on https://site-a.com from reading responses from https://api-b.com unless the API allows it:
Access-Control-Allow-Origin: https://site-a.com
Access-Control-Allow-Credentials: true
For "non-simple" requests (e.g. PUT, JSON body, custom headers) the browser first sends an OPTIONS preflight. CORS is enforced by the browser — Postman or a server can call the API freely.
HTTP versions
| Version | Key idea |
|---|---|
| HTTP/1.0 | new TCP connection per request |
| HTTP/1.1 | keep-alive (reuse connections), Host header; but head-of-line blocking — one slow response blocks the next on that connection |
| HTTP/2 | binary, multiplexing many streams on one TCP connection, header compression (HPACK), server push |
| HTTP/3 | runs on QUIC over UDP — no TCP head-of-line blocking, faster connection setup, survives network changes (Wi-Fi → mobile) |
HTTPS = HTTP over TLS
Plain HTTP is readable and changeable by anyone on the path (public Wi-Fi, ISP). HTTPS wraps HTTP in TLS, giving:
- Encryption — nobody can read the data.
- Integrity — nobody can change it unnoticed.
- Authentication — you are really talking to
codify.example(certificate).
Default ports: HTTP 80, HTTPS 443.
TLS 1.3 handshake (simplified)
Client ──ClientHello (TLS versions, ciphers, key share)──────────► Server
Client ◄──ServerHello (key share) + Certificate + Finished──────── Server
both compute the same session key (ECDHE)
Client ──Finished, then encrypted HTTP request──────────────────► Server
- Asymmetric crypto (public/private keys) is used only to agree on a key and prove identity.
- Symmetric crypto (e.g. AES-GCM, fast) encrypts the actual data with the shared session key.
- TLS 1.3 needs 1 round trip (TLS 1.2 needed 2); resumed sessions can use 0-RTT.
Certificates
A certificate binds a domain to a public key and is signed by a Certificate Authority (CA) (e.g. Let's Encrypt). The browser trusts a built-in list of root CAs and checks the chain: site cert → intermediate → root, plus the domain name and expiry. A mismatch shows "Your connection is not private".
HSTS (Strict-Transport-Security) tells browsers to always use HTTPS for the site, blocking downgrade attacks.
HTTP vs HTTPS
| HTTP | HTTPS | |
|---|---|---|
| Port | 80 | 443 |
| Encryption | ❌ | ✅ TLS |
| Certificate needed | ❌ | ✅ |
| SEO / browser warning | marked "Not secure" | preferred by search engines |
| Required for | — | service workers, geolocation, camera, HTTP/2 in browsers |
REST in one paragraph
REST APIs map resources to URLs and actions to methods: GET /notes, GET /notes/42, POST /notes, PATCH /notes/42, DELETE /notes/42. Use nouns, plural names, proper status codes, and JSON bodies. See Node.js and Express — Building a REST API.
Interview one-liners
- HTTP is stateless; cookies/tokens add state.
- GET, PUT and DELETE are idempotent; POST isn't.
- 401 = who are you? 403 = I know you, but no.
- HTTPS = HTTP + TLS: encryption, integrity, authentication.
- TLS uses asymmetric crypto to exchange a key, then symmetric crypto for data.
- HTTP/2 multiplexes over one TCP connection; HTTP/3 uses QUIC over UDP.
- Related: What Happens When You Type a URL, TCP vs UDP and the 3-Way Handshake.