Ecommet
Topic 12 of 75

HTTP and HTTPS

The protocol behind every web page and API: methods and status codes, safe and idempotent requests, caching headers, how HTTPS keeps traffic private, and what HTTP/2 and HTTP/3 changed.

Beginner 7 min read 4 quiz questions

Jump to the quiz

In a nutshell

  • HTTP is request/response: a method and a path go in, a status code, headers and a body come out.
  • GET is safe and idempotent, PUT and DELETE are idempotent, POST is neither: this decides what can be retried.
  • HTTPS = HTTP inside TLS: encrypted, tamper-proof, and the server proves who it is with a certificate.
  • HTTP/2 multiplexes requests over one connection; HTTP/3 runs on QUIC over UDP to avoid TCP head-of-line blocking.

A request and a response

HTTP is a text-based (in HTTP/1.1) conversation: the client sends a request, the server sends a response. Every API call and page load is one of these.

One HTTP exchange

One HTTP exchange Client → Server: GET /api/orders/42 (Accept: application/json) Server → Server: look up order 42 Server → Client: 200 OK (Content-Type: application/json) {…} ClientServerGET /api/orders/42 (Accept: application/json)look up order 42200 OK (Content-Type: application/json) {…}
The request names a method and a path; the response carries a status code, headers and a body.
Diagram as text
  • Client → Server: GET /api/orders/42 (Accept: application/json)
  • Server → Server: look up order 42
  • Server → Client: 200 OK (Content-Type: application/json) {…}

Requests have a method (what to do), a path (to what), headers (metadata such as content type, cookies, authentication) and sometimes a body. Responses have a status code, headers and a body.

Methods: safe and idempotent

Two properties of methods matter for system design, because they decide what can be cached and what can be retried after a timeout:

  • Safe: does not change anything on the server. Safe requests can be cached and prefetched.
  • Idempotent: doing it twice has the same effect as doing it once. Idempotent requests can be retried safely when you are not sure the first one arrived.
MethodUsed toSafeIdempotent
GETRead a resourceYesYes
HEADRead only the headersYesYes
PUTCreate or replace a resource at a known pathNoYes
DELETERemove a resourceNoYes
POSTCreate something or trigger an actionNoNo
PATCHPartly update a resourceNoNot guaranteed

Status codes

RangeMeaningOnes you will meet
2xxSuccess200 OK, 201 Created, 204 No Content
3xxRedirect or use the cache301 Moved Permanently, 302 Found, 304 Not Modified
4xxThe client got something wrong400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 409 Conflict, 429 Too Many Requests
5xxThe server failed500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable, 504 Gateway Timeout

The difference matters for clients: retrying a 4xx usually fails again (fix the request), while a 503 or 504 may succeed later. A 429 means slow down, often with a Retry-After header saying when.

Caching with HTTP headers

HTTP has caching built in. The server tells browsers and CDNs how long a response stays fresh, and how to check whether it changed:

  • Cache-Control: max-age=31536000, immutable for files whose name changes when their content does (such as app.3f9a1c.js): cache for a year.
  • Cache-Control: no-store for private or always-fresh data.
  • ETag and If-None-Match: the client asks "has it changed since this version?" and gets a tiny 304 Not Modified if not.

HTTPS: HTTP over TLS

Plain HTTP travels as readable text: anyone on the network path (public Wi-Fi, an ISP) can read or change it. HTTPS wraps HTTP in TLS, which gives three guarantees: encryption (nobody can read it), integrity (nobody can change it unnoticed) and authentication (the server proves its identity with a certificate signed by a trusted certificate authority).

TLS 1.3 handshake (simplified)

TLS 1.3 handshake (simplified) Browser → Server: ClientHello + key share Server → Browser: ServerHello + key share + certificate Browser → Browser: verify certificate, derive keys Browser → Server: Finished + encrypted GET / Server → Browser: encrypted 200 OK BrowserServerClientHello + key shareServerHello + key share + certificateverify certificate, derive keysFinished + encrypted GET /encrypted 200 OK

All steps One round trip after the TCP connection: agree keys, check the certificate, then send encrypted HTTP.

Diagram as text
  1. The browser says hello, lists the ciphers it supports and sends its half of a key exchange.
  2. The server replies with its half, its certificate and proof it holds the certificate's private key.
  3. The browser checks the certificate chain and the hostname, and both sides derive the same session keys.
  4. The browser finishes the handshake and can send its first encrypted request straight away.
  5. Everything from now on is encrypted with the session keys.

HTTPS is the default now: browsers mark plain HTTP pages as "Not secure", and free certificates (for example from Let's Encrypt) removed the cost. Load balancers and CDNs usually terminate TLS: they decrypt at the edge and talk to app servers over a private network (or re-encrypt for end-to-end security).

HTTP/1.1, HTTP/2 and HTTP/3

HTTP/1.1HTTP/2HTTP/3
Runs overTCPTCPQUIC over UDP
Requests per connectionOne at a time; browsers open several connections per hostMany at once (multiplexed streams)Many at once, with independent streams
HeadersPlain text, repeated on every requestCompressed (HPACK)Compressed (QPACK)
Head-of-line blockingPer connectionGone at the HTTP level, but one lost TCP packet stalls all streamsGone: a lost packet only stalls its own stream
Connection set-upTCP + TLSTCP + TLSCombined: as little as one round trip, zero on resumption

HTTP/3 also survives network changes better: a phone switching from Wi-Fi to mobile data can keep the same QUIC connection, because QUIC identifies connections by an ID rather than by IP address and port.

Common mistakes

  • Using GET for actions that change data. Crawlers, prefetchers and caches will happily trigger them.
  • Retrying POST requests automatically without idempotency keys.
  • Returning 200 with an error message in the body: clients, caches and monitoring all rely on status codes.
  • Caching personalised responses at a CDN because Cache-Control was left to defaults.

Interview questions

Try answering out loud before you open each one.

What is the difference between PUT and POST?

PUT creates or replaces the resource at a URL the client specifies, and is idempotent: sending it twice leaves the same state. POST asks the server to create something or perform an action, usually at a URL the server chooses; sending it twice may create two resources, so it is not idempotent.

Why is HTTP/2 faster than HTTP/1.1 for a page with many assets?

HTTP/1.1 handles one request at a time per connection, so browsers open several connections and assets queue behind each other. HTTP/2 multiplexes many requests over one connection with compressed headers, removing that queueing and the cost of extra connections.

What does HTTPS protect against, and what not?

It prevents eavesdropping and tampering between the client and the server that terminates TLS, and proves the server's identity. It does not protect against a compromised server, bugs in your app, or a user who ignores certificate warnings, and the destination hostname is still visible to the network in most cases.

Check yourself

1. Which method is idempotent but not safe?
Show the answer

Answer: PUT. PUT changes state (not safe), but repeating it leaves the same state (idempotent).

2. A client gets 503 Service Unavailable. What should it usually do?
Show the answer

Answer: Retry later, with backoff. 5xx errors are server-side and often temporary; retry with backoff (and respect Retry-After).

3. What does a 304 Not Modified response tell the client?
Show the answer

Answer: Its cached copy is still valid. The client sent a validator such as If-None-Match, and the resource has not changed.

4. What does HTTP/3 run on?
Show the answer

Answer: QUIC over UDP. HTTP/3 uses QUIC, a transport built on UDP with its own reliability and encryption.

Further reading

Your notes

Saved in this browser as you type.

Finished this topic?

Mark it read to track your progress, then carry on with the next one.

Next topic