What is HTTP?

In short
HTTP, short for HyperText Transfer Protocol, is the set of rules a browser and a server use to exchange messages on the Web. The browser sends a request for a resource, and the server sends back a response. HTTP sits at the application layer and relies on a transport protocol such as TCP to deliver those messages.
How the client and server talk
HTTP is a client-server protocol. The client starts every exchange by sending a request, and a server answers with a response. In most cases the client is a web browser, but it can be any program that acts for a user, such as a crawler that collects pages for a search engine or a tool that developers use to test their apps.
A single web page is rarely one request. The browser first asks for the HTML document. While it reads that file, it sends more requests for styles, scripts, images and videos. Then it combines everything into the page you see. Links on the page are also HTTP requests waiting for a click.
Between the browser and the server, many machines may relay the messages. These middlemen are called proxies. A proxy can cache responses so that the next person gets them faster, filter traffic, balance load across several servers, check who is allowed in, or keep logs. Most network hardware works below HTTP, so HTTP mostly does not see it.
Why HTTP is easy to work with
HTTP messages are written as plain text in the early versions of the protocol. A developer can read a request and a response without special tools. Later versions, starting with HTTP/2, pack messages into binary frames for speed, so you need a tool to read them. The meaning of the messages stays the same.
HTTP also lets you add new features through headers. A header is a line of extra information, such as the language you prefer or how long a document may be cached. Client and server can agree on what a new header means, which makes the protocol easy to extend.
Stateless, but not forgetful
Each HTTP request is independent. The protocol itself does not remember that the previous request came from the same user. This can be a problem for things like a shopping basket.
The fix is cookies. A cookie is a small piece of data that the server asks the browser to store and send back with later requests. With cookies, the server can link many requests to one session, so the site appears to remember you.
How connections work
HTTP usually runs on top of TCP, a transport protocol that delivers data reliably and needs a connection to be set up first. Setting up that connection takes several round trips between the client and the server, so it has a cost.
HTTP/1.0 opened a new connection for every request and response pair. HTTP/1.1 added persistent connections, which let several requests reuse one connection. HTTP/2 went further and sends multiple messages at the same time over a single connection. Google has also experimented with QUIC, a newer transport built on UDP, to make this faster.
A simple example
When you type an address into a browser, a request like this is sent:
GET / HTTP/1.1 Host: developer.mozilla.org Accept-Language: fr
The first line gives the method, the path and the version. The Host header names the site, since one server can host several sites. The server replies with a status line such as HTTP/1.1 200 OK, followed by headers like Content-Type and Content-Length, then the body with the page itself.
Common mistakes
Beginners often think HTTP keeps state on its own. It does not, so use cookies or another mechanism when you need a session. Another mistake is to assume the connection stays open forever. Connections can be closed or reused, and your code should not depend on either behavior.
If you want to learn the layers underneath HTTP, read about TCP and how the Internet is built in layers. Our pages on web servers are a good next step for seeing what happens on the other side of the request.
FAQ
Is HTTP the same as HTTPS?
HTTPS is HTTP sent over a secure connection. The messages keep the same structure, but the data is protected from people who might intercept it on the way.
Why is HTTP called stateless?
Each request is handled on its own, and the protocol does not link one request to the next. Cookies are used to add memory on top of it.
Do I need to read HTTP messages by hand?
Not usually, but it helps when debugging. Browser developer tools and command line clients can show you the requests and responses your app sends.
Which transport protocol does HTTP use?
Classic HTTP relies on TCP, which is reliable and connection-based. The protocol only needs the transport to be reliable, so newer options such as QUIC can also be used.
Sources
This page is based on the official documentation below. Read it for full detail.
HTTP — official documentation ↗