HTTP vs HTTPS: Methods, Status Codes and TLS Handshake

How HTTP works: requests and responses, methods, status codes, headers, cookies and sessions, HTTP/1.1 vs HTTP/2 vs HTTP/3, and how HTTPS adds TLS on top.

What is the difference between HTTP and HTTPS?

HTTP (Hypertext Transfer Protocol) is the stateless request-response protocol that browsers, apps and servers use to exchange web pages and API data, by default on TCP port 80. HTTPS is the same HTTP carried inside a TLS-encrypted connection, by default on port 443. TLS adds encryption, integrity checking and server authentication through certificates, so an eavesdropper on the network can neither read nor silently change the traffic.

HTTP is the protocol of the web: every page, image, script and API call your browser makes is an HTTP request answered by an HTTP response. It is simple on purpose (text messages in its classic form, one request and one response) and stateless, which is what lets it scale to billions of users. HTTPS is not a different protocol; it is HTTP sent through an encrypted TLS tunnel. This note covers the message format, methods, status codes, state, the three HTTP versions and the TLS handshake.

How HTTP works

HTTP is a client-server, request-response protocol at the application layer. The client (a browser, an app, curl) opens a connection to the server, sends a request and reads the response. HTTP/1.1 and HTTP/2 run over TCP; HTTP/3 runs over QUIC on UDP. The resource is named by a URL:

Part of https://www.example.com:443/search?q=tcp#resultsValuePurpose
SchemehttpsProtocol, and so the default port (80 for http, 443 for https)
Hostwww.example.comResolved by DNS to an IP address
Port443Usually left out when it is the default
Path/searchWhich resource on the server
Queryq=tcpParameters as key=value pairs
FragmentresultsA position inside the page; never sent to the server

The request and the response

A request has a request line (method, path, version), headers, a blank line and an optional body. A response has the same shape with a status line (version, status code, reason phrase) in front.

clientserverHTTP/1.1 201 CreatedLocation: /api/users/43Content-Type: application/jsonContent-Length: 50{"id":43,"name":"Asha","email":"asha@example.com"}status lineheadersblank linebody50 bytes = Content-Length
Anatomy of an HTTP request and its response. Example: POST /api/users, answered with 201 Created
  1. The request: a request line with the method, the path and the version, then headers one per line, then a blank line, then the body. Content-Length tells the server where the body ends: these 42 bytes.
  2. The response has the same shape with a status line in front: the version, the status code 201 and its reason phrase. Location names the user just created, and the body is 50 bytes.

Content-Length is the body's size in bytes, which is how the reader knows where the body ends. In HTTP/2 and HTTP/3 the same fields travel as binary frames, but the meaning is identical.

HTTP methods

A method is safe if it does not change server state, and idempotent if sending it once or many times leaves the server in the same state.

MethodPurposeSafeIdempotentBody
GETRead a resourceYesYesNo
HEADLike GET, but headers onlyYesYesNo
OPTIONSAsk what the server allows (used by CORS preflight)YesYesRarely
POSTCreate a resource or trigger processingNoNoYes
PUTCreate or fully replace the resource at this URLNoYesYes
PATCHChange part of a resourceNoNot guaranteedYes
DELETERemove a resourceNoYesRarely

Idempotence is about state, not the response: a second DELETE of the same item may return 404, but the server is in the same state as after the first. This is why clients and proxies may retry idempotent requests after a network failure but must not blindly retry a POST.

Status codes

ClassMeaningCommon codes
1xxInformational100 Continue, 101 Switching Protocols (WebSocket upgrade)
2xxSuccess200 OK, 201 Created, 204 No Content
3xxRedirection301 Moved Permanently, 302 Found, 304 Not Modified, 307 Temporary Redirect, 308 Permanent Redirect
4xxClient error400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 405 Method Not Allowed, 409 Conflict, 429 Too Many Requests
5xxServer error500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable, 504 Gateway Timeout

The pairs interviewers ask about:

  • 401 vs 403: 401 means "authenticate first"; 403 means "you are known, and still not allowed".
  • 301 vs 302: 301 is permanent (browsers and search engines remember the new URL), 302 is temporary. 307 and 308 are their counterparts that forbid changing the method, so a redirected POST stays a POST.
  • 502 vs 504: both come from a gateway or proxy; 502 means the upstream server sent an invalid response, 504 means it did not answer in time.
  • 304 Not Modified: the client's cached copy is still valid, so no body is sent.

Headers

HeaderSent inMeaning
HostRequestWhich website on this IP address; mandatory in HTTP/1.1
User-AgentRequestThe client software
Accept, Accept-EncodingRequestFormats and compressions (gzip, br) the client can take
AuthorizationRequestCredentials, such as Bearer followed by a token
Content-Type, Content-LengthBothThe body's format and size
Cookie, Set-CookieRequest, responseCarry state; see below
Cache-ControlBothCaching rules: max-age=600, no-store
ETag, If-None-MatchResponse, requestA version tag; if it still matches, the server answers 304
LocationResponseWhere a redirect points, or the URL of a created resource
Connection: keep-aliveBoth (HTTP/1.x)Reuse the TCP connection for more requests

Statelessness, cookies and sessions

HTTP itself remembers nothing between requests. To keep a user logged in, the server sends a cookie, and the browser returns it on every later request to that site.

browserserversession storePOST /loginsave 4f2a9c → Asha200 OKSet-Cookie: session=4f2a9cGET /cartCookie: session=4f2a9clook up 4f2a9cAsha200 OK: Asha's cart
How a cookie turns stateless requests into a logged-in session. Example: log in, then fetch the cart
  1. The login request succeeds. The server stores the user under a random session ID, 4f2a9c, and sends that ID back in a Set-Cookie header; the browser files the cookie under this site.
  2. Later, a completely separate request: HTTP itself remembers nothing of the login. But the browser attaches the cookie automatically to every request it sends to this site.
  3. The server looks the ID up, finds Asha, and answers as if the conversation had continued. A request with an unknown ID, such as session=000000, would get 401 Unauthorized.

A real Set-Cookie header also carries attributes that limit where and how the cookie travels:

Set-Cookie: session=4f2a9c; Path=/; Max-Age=86400; HttpOnly; Secure; SameSite=Lax
AttributeEffect
Max-Age or ExpiresHow long the cookie lives; without either, it is deleted when the browser closes
Domain, PathWhich hosts and paths receive it
SecureSent only over HTTPS
HttpOnlyHidden from JavaScript, which limits theft through cross-site scripting
SameSiteStrict, Lax or None: whether it is sent on cross-site requests, a defence against cross-site request forgery

With a server-side session, as in the figure, the cookie holds only a random ID and the server keeps the user's data in memory, a database or a cache such as Redis. With token-based authentication, the client holds a signed token (such as a JWT) carrying the user's identity, and the server only verifies the signature. Sessions are easy to revoke; tokens need no lookup but are harder to cancel before they expire.

HTTP/1.1 vs HTTP/2 vs HTTP/3

AspectHTTP/1.1 (1997)HTTP/2 (2015)HTTP/3 (2022)
TransportTCPTCP, with TLS in practiceQUIC over UDP
FormatTextBinary framesBinary frames
ConcurrencyOne request at a time per connection; browsers open about six connections per hostMany streams multiplexed on one connectionMany independent streams on one connection
Header compressionNoneHPACKQPACK
Head-of-line blockingAt the HTTP levelFixed at the HTTP level, but one lost TCP packet stalls all streamsRemoved: a loss stalls only its own stream
Connection setupTCP, then TLSTCP, then TLSQUIC and TLS 1.3 together in one round trip
HTTP/1.1waitcsswaitjswaitpngHTTP/2waitcsspngjspng04080120160200240280ms
Three files over one connection: HTTP/1.1 against HTTP/2. Example: style.css, app.js (slow to generate), logo.png; round trip 40 ms
  1. HTTP/1.1 on one connection: a request goes out only after the previous response has fully arrived, and each costs a round trip plus the server's time. The slow app.js holds up logo.png behind it; all three are in at 300 ms.
  2. HTTP/2 sends all three requests at once and interleaves the responses as frames on one connection. While app.js is still being generated, the CSS and the image flow past it, and everything is in at 140 ms.

HTTP/1.0 (1996) opened a new connection for every request. HTTP/1.1 made persistent connections the default and added the mandatory Host header (so many sites can share one IP address) and chunked transfer. Its pipelining feature allowed several requests in a row, but responses had to come back in order and browsers left it switched off. HTTP/2's server push was later dropped by browsers. HTTP/3 also survives a change of network, such as Wi-Fi to mobile data, because QUIC identifies a connection by an ID rather than by IP addresses and ports.

HTTPS and the TLS handshake

HTTPS gives three guarantees: confidentiality (nobody on the path can read the data), integrity (any change is detected) and authentication (a certificate proves the server owns the domain). TLS 1.3 sets all three up in one round trip after TCP's:

clientserverSYNSYN-ACK50 msACKClientHello + key share, SNIServerHello + key share{Certificate}{CertificateVerify}{Finished}100 ms{Finished}{GET /}{200 OK …}150 msround trip 1round trip 2round trip 3
HTTPS with TLS 1.3: three round trips to the first byte. Example: a new connection, round trip 50 ms
  1. TCP first: SYN and SYN-ACK cost one round trip, so the client's clock reads 50 ms before any TLS message has been sent.
  2. The client's ACK goes out with the ClientHello right behind it: the versions and ciphers it supports, its Diffie-Hellman key share and the server name it wants (SNI).
  3. The server answers with its own key share. Both sides can now compute the same secret, so everything after the ServerHello is encrypted: the certificate, a signature proving the server holds its private key, and Finished. That is round trip 2, at 100 ms.
  4. The client checks the certificate chain, dates and host name and the signature, sends its own Finished, and puts the first HTTP request on the same flight, already encrypted.
  5. The response's first byte arrives after 3 round trips, 150 ms. TLS 1.2 needed a second handshake round trip, so the same request took 200 ms; on a reused connection only the last round trip remains.

The two key shares are the halves of an ephemeral Diffie-Hellman exchange: each side combines its own private value with the other's share and arrives at the same shared secret, from which both derive the session keys, without the secret ever crossing the network. The certificate check is what stops an impostor: the chain must lead to a certificate authority the client trusts, the dates must be valid, the names must cover the requested host and it must not be revoked, and the CertificateVerify signature proves the server holds the matching private key. Application data then flows under a fast symmetric cipher such as AES-GCM or ChaCha20-Poly1305.

TLS 1.2 needed two round trips and also allowed RSA key exchange, where the client encrypts a secret with the server's public key. That lacks forward secrecy: anyone who later steals the server's private key can decrypt recorded traffic. TLS 1.3 removed it; every key exchange is ephemeral. Asymmetric cryptography is used only to agree keys and prove identity, because it is far slower than symmetric encryption; see network security basics.

Worked example: round trips before the first response byte

Take a round-trip time of 50 ms and ignore DNS and server processing time.

SetupRound tripsTime
HTTP over TCP1 (TCP handshake) + 1 (request and response) = 2100 ms
HTTPS, TLS 1.21 (TCP) + 2 (TLS) + 1 (request) = 4200 ms
HTTPS, TLS 1.31 (TCP) + 1 (TLS) + 1 (request) = 3150 ms
HTTP/3 over QUIC1 (QUIC with TLS) + 1 (request) = 2100 ms

On a reused connection only the last round trip remains, which is why keep-alive and connection reuse matter so much.

HTTPS does not hide everything: the IP addresses, the timing and size of traffic, the DNS lookup and normally the server name in the ClientHello (unless Encrypted Client Hello is used) are visible. The path, query string, headers, cookies and body are encrypted.

REST basics

REST (Representational State Transfer) is a style for designing APIs on top of HTTP: resources are named by URLs (nouns), HTTP methods are the verbs, each request carries everything needed to process it (stateless), and resources are transferred as representations, usually JSON.

ActionRequestTypical success response
List usersGET /users200 OK
Read one userGET /users/42200 OK (404 if absent)
Create a userPOST /users201 Created, with Location: /users/43
Replace a userPUT /users/42200 OK or 204 No Content
Change one fieldPATCH /users/42200 OK
Delete a userDELETE /users/42204 No Content

Common mistakes

  • Saying GET is secure because parameters are "hidden" in POST: both are readable over HTTP and both are encrypted over HTTPS; GET parameters do end up in logs and history.
  • Calling PATCH idempotent by definition: only GET, HEAD, OPTIONS, PUT and DELETE (and TRACE) are.
  • Treating 401 and 403 as the same.
  • Saying HTTPS encrypts the domain name: DNS and SNI normally reveal it.
  • Saying HTTP/2 removed head-of-line blocking completely: it remains at the TCP level; HTTP/3 removes it.
  • Describing the TLS handshake as "the client encrypts the data with the server's public key": the public key authenticates and helps agree a symmetric key; the data is encrypted symmetrically.

Interview questions

What is idempotency, and which HTTP methods are idempotent? A request is idempotent if repeating it leaves the server in the same state as sending it once. GET, HEAD, OPTIONS, PUT and DELETE are idempotent; POST is not, and PATCH is not guaranteed to be. It matters because idempotent requests can be retried safely after a timeout.

How do cookies make a stateless protocol stateful? The server sends a Set-Cookie header with an identifier, and the browser automatically includes it in every later request to that site. The server uses the identifier to look up the session, so a sequence of independent requests behaves like one logged-in conversation.

What did HTTP/2 improve over HTTP/1.1? It replaced text with binary framing and multiplexes many concurrent streams over a single TCP connection, so one slow response no longer blocks the others at the HTTP level and browsers need fewer connections. It also compresses headers with HPACK, saving bytes on repeated headers such as cookies.

Why does HTTP/3 run on UDP? TCP delivers bytes strictly in order, so a single lost packet stalls every HTTP/2 stream sharing the connection. QUIC, built on UDP, implements reliability per stream, so a loss affects only its own stream. It also merges the transport and TLS handshakes and can move a connection between networks.

Explain the TLS handshake. The client and server exchange supported versions, random numbers and Diffie-Hellman key shares, and both compute the same secret from which session keys are derived. The server proves its identity with its certificate and a signature made with the matching private key, both sides confirm the handshake with Finished messages, and the data then flows under symmetric encryption.

What does a certificate prove, and how does a browser check it? It binds a public key to a domain name, signed by a certificate authority. The browser follows the chain of signatures up to a root certificate in its trust store, checks the validity dates, checks that the certificate's names match the host and checks revocation; then the server must prove it holds the private key.

What is the difference between 502 and 504? Both are returned by a gateway, proxy or load balancer. 502 Bad Gateway means the upstream server returned an invalid or broken response; 504 Gateway Timeout means the upstream server did not respond within the proxy's time limit.

Next, read routing and routing protocols, and test yourself with the Computer Networks (Intermediate) skill test.

Common questions

What does it mean that HTTP is stateless?

Each request is handled on its own; the server keeps no memory of earlier requests from the same client as part of the protocol. Applications add state on top, usually with a cookie that carries a session ID or a signed token, which the browser sends back with every request.

What is the difference between GET and POST?

GET reads a resource; its parameters travel in the URL's query string, and it is safe and idempotent, so it can be cached, bookmarked and retried. POST sends data in the request body to create something or trigger processing; it is neither safe nor idempotent, so repeating it may create a duplicate.

What is the difference between 401 and 403 status codes?

401 Unauthorized means the request lacks valid authentication: the client has not proved who it is, or its credentials are wrong or expired. 403 Forbidden means the server knows who the client is, or does not need to, but refuses access anyway. Logging in can fix a 401 but not a 403.

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

HTTP/1.1 is text-based and handles one request at a time per TCP connection. HTTP/2 uses binary frames to multiplex many requests over one TCP connection and compresses headers. HTTP/3 runs over QUIC on UDP, so one lost packet no longer stalls every stream, and its handshake includes TLS 1.3.

Is HTTPS slower than HTTP?

Only slightly, at connection setup: TLS 1.3 adds one round trip (TLS 1.2 added two), and resumed sessions can skip most of it. Once connected, symmetric encryption is cheap on modern processors. Browsers also allow HTTP/2 and HTTP/3 only over encrypted connections, so HTTPS sites are often faster overall.

What is the difference between PUT and PATCH?

PUT replaces the whole resource at a URL with the body sent, and repeating it gives the same result, so it is idempotent. PATCH applies a partial change, such as updating one field; it is not guaranteed to be idempotent, because a patch like "add 1 to the counter" changes the result each time.

Test yourself

← Domain Name System (DNS) · Routing and Routing Protocols →