HTTP & HTTPS

HTTP (HyperText Transfer Protocol) is the language a browser and a web server use to ask for and deliver pages, images, JSON and files. Every web API you call from an app speaks it too. HTTPS is the same protocol wrapped in an encrypted tunnel called TLS. This lesson shows what an HTTP message looks like on the wire, what the codes and headers mean, and what actually happens when you see the padlock.

What Is HTTP?

HTTP is a request and response protocol. The client (a browser, a mobile app, or a command-line tool such as curl) opens a connection, sends one request, and the server sends back one response. Three ideas are worth remembering:

  • The client always speaks first. A server cannot push a message to a plain HTTP client that did not ask (that is the gap WebSocket fills).
  • It is stateless. Each request stands alone. The server does not remember you between requests unless you send something that identifies you, such as a cookie or a token.
  • It rides on TCP. HTTP relies on TCP to deliver the bytes reliably and in order. Plain HTTP uses port 80, HTTPS uses port 443.

Anatomy of a Request and a Response

In HTTP/1.1 both messages are plain text: a first line, some headers, an empty line, and an optional body.

One HTTP request and its response The browser sends a GET request for /products with a Host header. The server answers with status 200 OK, a Content-Type header, a Content-Length header and the HTML page. Browser (client) Web server 1. Request GET /products HTTP/1.1 Host: example.com Accept: text/html 2. Response HTTP/1.1 200 OK Content-Type: text/html Content-Length: 5120 <html> ... </html>
A request names a method and a path; the response starts with a status code. The empty line before the HTML is what separates headers from the body.
PartWhat it holds
Request lineThe method, the path (and query string), and the HTTP version, for example GET /products?page=2 HTTP/1.1
Status lineThe version, a three-digit status code and a short reason, for example HTTP/1.1 404 Not Found
HeadersLines of Name: value that describe the message: who you are, what format the body is in, how long it is, how to cache it
Empty lineMarks the end of the headers
BodyThe actual content: a form, JSON, an image. Many requests (such as a plain GET) have no body at all

The Common Methods

The method says what you want to do with the resource. Two properties matter to API designers: a method is safe if it only reads and changes nothing on the server, and idempotent if sending it ten times has the same effect as sending it once.

MethodTypically used toRequest bodySafeIdempotent
GETRead a resourceNoYesYes
POSTCreate something or submit dataYesNoNo
PUTReplace a resource completelyYesNoYes
PATCHChange part of a resourceYesNoNot guaranteed
DELETERemove a resourceRarelyNoYes
HEADLike GET, but only the headers come backNoYesYes
OPTIONSAsk what the server allows (browsers use it for CORS preflight checks)NoYesYes

Status Codes

The first digit tells you the family; the rest tells you the detail.

FamilyMeaningCodes you will meet most
1xxInformational101 Switching Protocols (used when a connection upgrades to WebSocket)
2xxSuccess200 OK, 201 Created, 204 No Content
3xxRedirection301 Moved Permanently, 302 Found, 304 Not Modified (use your cached copy)
4xxThe client made a mistake400 Bad Request, 401 Unauthorized (not logged in), 403 Forbidden (logged in but not allowed), 404 Not Found, 405 Method Not Allowed, 429 Too Many Requests
5xxThe server failed500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable, 504 Gateway Timeout

Headers You Will See Everywhere

HeaderPurpose
HostWhich website the request is for. One server can host many sites on one IP address
Content-TypeThe format of the body, such as application/json or text/html; charset=utf-8
Content-LengthThe size of the body in bytes
AcceptThe formats the client is willing to receive
AuthorizationCredentials, for example Bearer <token>
Cookie / Set-CookieHow a server asks a browser to remember something, and how the browser sends it back
Cache-ControlWhether and for how long a response may be reused without asking again
LocationWhere to go next, in a redirect
User-AgentWhich browser or tool is making the request

Why Plain HTTP Is Not Safe

With plain HTTP the text of the request and the response travels exactly as shown above. Everyone on the path between you and the server can read it: other people on the same café Wi-Fi, a router, your internet provider. They can also change it on the way. Passwords, session cookies and form data would be visible, and a page could be altered before it reaches you. That is the problem HTTPS solves.

HTTPS = HTTP + TLS

HTTPS does not change HTTP at all. The same requests and responses are simply sent inside a TLS (Transport Layer Security) session, which sits between HTTP and TCP.

Where TLS sits between HTTP and TCP Two layer stacks side by side. Plain HTTP is HTTP over TCP on port 80, over IP, over Wi-Fi or Ethernet. HTTPS adds a TLS layer between HTTP and TCP and uses port 443. HTTP (port 80) HTTPS (port 443) HTTP requests and responses, in plain text TCP reliable delivery, port 80 IP addresses and routing Wi-Fi or Ethernet the physical link Anyone on the path can read this HTTP the same requests and responses TLS encrypts, checks, proves identity TCP reliable delivery, port 443 IP addresses and routing Wi-Fi or Ethernet the physical link Only the two ends can read this
HTTPS is HTTP with one extra layer. Everything above TLS is encrypted before it leaves your device.

TLS makes three promises:

  • Privacy: the traffic is encrypted, so an eavesdropper only sees scrambled bytes.
  • Integrity: any change to the data on the way is detected.
  • Identity: a certificate proves you are talking to the real owner of the domain, not an impostor.

The TLS Handshake

Before any HTTP is sent, the two sides agree on the settings and on a secret key. With TLS 1.3, the version used by most sites today, this takes one round trip after the TCP connection is open.

TCP handshake followed by the TLS 1.3 handshake The browser and the server first complete the three-step TCP handshake. Then the browser sends a ClientHello, the server answers with a ServerHello and, encrypted, its certificate and Finished message, the browser sends its own Finished message, and encrypted HTTP traffic flows in both directions. Browser Server TCP SYN SYN, ACK ACK TLS 1.3 ClientHello TLS version, cipher suites, key share ServerHello chosen cipher suite, key share Certificate + proof + Finished (encrypted) Finished (encrypted) Encrypted HTTP request and response the same TCP connection, now protected TLS 1.3 needs one round trip before the first encrypted request. TLS 1.2 needed two.
The browser does not trust the server yet at the ClientHello. By the time the server's Finished message arrives, both sides share a key and the browser has checked the certificate.
  1. ClientHello. The browser lists the TLS versions and cipher suites it supports and sends its half of a key exchange. It also names the website it wants (the "server name").
  2. ServerHello. The server picks a cipher suite and sends its half of the key exchange. From this point both sides can compute the same secret key, and everything that follows is encrypted.
  3. Certificate and proof. The server sends its certificate and a signature that proves it holds the matching private key, then a Finished message that covers the whole conversation so far.
  4. Finished. The browser verifies the certificate and the Finished message, and sends its own. Now the first HTTP request can go out, encrypted.

A detail that surprises many people: slow public-key maths is used only to agree on a shared secret. The actual page data is then encrypted with a fast symmetric cipher such as AES-GCM or ChaCha20-Poly1305. The key exchange is also done with fresh temporary keys each time, so recording today's traffic and stealing the server's key next year does not let anyone decrypt it (this is called forward secrecy).

Certificates and Who to Trust

Encryption alone is not enough: you could be encrypting a conversation with an attacker. A certificate says "this public key belongs to example.com", and it is signed by a certificate authority (CA). Your browser and operating system ship with a list of root CAs they trust. A website usually sends its own certificate plus an intermediate one, and the browser follows the signatures up to a root it already knows.

A certificate chain A root certificate authority signs an intermediate certificate authority, which signs the website certificate for example.com. The root is already in the browser's trust store. Root CA self-signed kept in your trust store Intermediate CA signed by the root sent by the server example.com signed by the intermediate domain names and dates signs signs The browser checks every signature from the website back up to a root it already trusts, that the name matches the address you typed, and that the dates are still valid.
A chain of trust: only the root needs to be pre-installed, and a website can prove itself through the chain.

Certificates are valid for a limited time (months, not years) and must be renewed. Free certificate authorities such as Let's Encrypt made HTTPS the default for almost every site.

What HTTPS Does Not Hide

  • Which site you visit. The domain name is usually visible through the DNS lookup and the server name in the ClientHello. The page you opened and the data you sent are hidden; the fact that you contacted example.com is not.
  • The IP address and the amount and timing of traffic. An observer can still see roughly how much you sent and when.
  • Whether the website itself is honest. A phishing site can have a perfectly valid HTTPS certificate for its own domain. The padlock means "private connection to this domain", not "this domain is trustworthy".
Note: The command-line examples below are meant to be run on your own computer, not on this website.

HTTP Versions at a Glance

VersionWhat changed
HTTP/1.1Text-based messages, as shown in this lesson. Connections can be kept open for several requests, but responses on one connection arrive in order
HTTP/2Binary framing, many requests multiplexed over one connection, and compressed headers. Browsers use it only over TLS
HTTP/3Runs over QUIC, which is built on UDP instead of TCP. It sets up connections faster and a lost packet no longer stalls every other request (see TCP vs UDP)

The meaning of methods, status codes and headers is the same in all three versions. Only the way the messages are packed for the network changes.

Try It Yourself

The curl tool can show you a whole HTTPS exchange. The -v flag prints the handshake and both messages:

curl -v https://example.com

The output differs between curl versions and operating systems, but you will see lines starting with * (connection and TLS details), > (the request curl sent) and < (the response it received), similar to:

* Connected to example.com port 443
* TLSv1.3 (OUT), TLS handshake, Client hello
* TLSv1.3 (IN), TLS handshake, Server hello
> GET / HTTP/2
> Host: example.com
< HTTP/2 200
< content-type: text/html

To see only the response headers, use curl -I https://example.com. In JavaScript the same request looks like this:

fetch('https://api.example.com/items/1')
  .then(function (response) {
    console.log(response.status);        // 200
    return response.json();
  })
  .then(function (item) {
    console.log(item);
  });

Common Mistakes

  • Mixed content. Loading a script or image over http:// from an HTTPS page. Browsers block or warn about it.
  • An expired or mismatched certificate. The browser shows a full-page warning. Renew on time and make sure the certificate covers the exact domain name you use.
  • Leaving plain HTTP reachable. Redirect http:// to https://, and consider the Strict-Transport-Security header so browsers go straight to HTTPS next time.
  • Secrets in the URL. Query strings end up in logs, history and referrer headers, even over HTTPS. Send tokens in a header and sensitive data in the body.
  • Using GET to change data. Crawlers, prefetchers and caches assume GET is safe. Use POST, PUT or DELETE for changes.
Recap: HTTP is a text-based request and response protocol over TCP: a method and path out, a status code and body back, with headers describing both. It is stateless and the client always speaks first. HTTPS is the same HTTP inside TLS: a handshake agrees on keys, a certificate chain proves the server's identity, and the traffic is then encrypted and tamper-proof. It hides the content, not the fact that you visited the site.
Next steps: Continue to WebSocket to see how an HTTP request becomes a two-way connection. To call HTTP APIs from an app, see Android HTTP Basics.