Skip to content

HTTP

To get two computers to communicate over a network, we need to agree on a protocol.

A protocol describes messages exchanged between multiple systems. This includes the format of the messages themselves, as well as how they are transmitted.

Example Protocol

Protocols can be thought of as a dialog between two systems:

Client: MENU

Server:

               pizza  3.50            
               cookie 1.50
               chips  1.00
               juice  1.50
               water  1.00

Client: BUY pizza water

Server: PAY 4.50

Client: CARD 4.50 5555-2222-1111-3333

Server: CONFIRMED

The protocols themselves are arbitrary; what matters is that we have agreed upon the meaning of the messages in advance.

The most important and ubiquitous protocol today is HTTP.

Requests & Responses

HTTP was created for the web, originally to exchange HTML pages, but today powers much more.

When you visit a webpage, either by typing in the URL or clicking a link, your browser makes a request to a web server. The server then sends a response message.

Client sends HTTP request:

GET /index.html HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0
Accept: */*

Server sends HTTP response:

HTTP/1.1 200 OK
Content-Encoding: gzip
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Sun, 12 Jan 2025 10:00:00 GMT
Etag: "3147526947+gzip"
Expires: Sun, 19 Jan 2025 10:00:00 GMT
Last-Modified: Thu, 17 Oct 2024 07:18:26 GMT
Server: ECS (sec/96A4)
Content-Length: 648

<!doctype html>
<html>
<head>
    <title>Example Domain</title>
</head>
<body>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents...</p>
</body>
</html>

Every time your browser loads a typical web page it makes many such requests. There will be at least one for the structure of the page, which comes back in HTML, as well as additional requests for other data and outside resources such as images.

HTTP Requests

An HTTP request is a block of formatted text that consists of several parts:

  • Verb: GET, POST, HEAD, PUT, DELETE, PATCH
  • URI: Everything after the domain name (e.g. /api/v3/hearings/?api_key=12345)
  • Protocol Version: e.g. HTTP/1.1
  • Request Headers: String Key-value pairs, one per line
    • User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Firefox/102.0
    • Accept-Language: de
    • Cookie: name=value; name2=value2; name3=value3
  • Body: Used for parameters in POST and other methods.

HTTP Verbs

Within the browser almost all requests will be either GET or POST.

A GET request is intended to fetch content from the server.

GET requests can typically be thought of as "read only", a contract between the client and server that the server should not need to change anything—merely reply with content.

A POST request on the other hand is meant to be used to make a change on the server. It may also be used to send data to the server, such as when a user submits their username and password to log in.

To put these in familiar context, when you browse a webpage, clicking links/etc. you are making GET requests. When you fill out a form, sending data to the server, you are often making a POST request.

URIs/Query Strings

After the verb a request will list a URI, a path to the resource being requested.

These take the form of slashed paths like:

  • /index.html
  • /users/Paul/
  • /api/v3/hearings/house/118/

They can also contain a section known as a query string, which begins with a ? character. These query strings consist of key-value pairs separated by & characters.

  • /search**?q=python**
  • /page/?page=2&lang=en
  • /api/v3/hearings/house/118/?format=json&api_key=12345&offset=40

Taken together with the domain name, these form the full URL that you would see in your browser.

But you'll notice that the domain name is not included in the URI.

It is instead part of the next section, the HTTP headers.

HTTP Headers

Taking a look at our initial request:

GET /index.html HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0
Accept: */*

The first line contains the verb, URI, and protocol.

The lines immediately following all take the form:

Key: Value

These are called headers, and provide a way for the browser (or any other HTTP client) to send supplemental information to the server that is not part of the URI.

HTTP defines a set of headers such as Host, User-Agent, and Accept.

  • Host is required and contains the domain name of the request.
  • User-Agent is almost always present. It is the client's way of identifying itself to the server.

You'll often see other headers as well, such as the Accept we include here, these allow the client to make additional restrictions on its request such as asking the server to send pages back in a particular format or language.

See MDN: Request Headers for more details.

When it comes to user-defined headers, they may be used for anything the server chooses. This means while sometimes you'll see parameters in the query string, some services will instead ask you to send them in the headers.

HTTP Responses

Once a request is sent, the client will wait for a response. If the server is not available, or too slow to respond, you may see a timeout. But assuming all is working as intended, the server will respond in a format similar to the initial request.

Taking a look at our response:

HTTP/1.1 200 OK
Content-Encoding: gzip
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Sun, 12 Jan 2025 10:00:00 GMT
Expires: Sun, 19 Jan 2025 10:00:00 GMT
Last-Modified: Thu, 17 Oct 2024 07:18:26 GMT
Content-Length: 648

<!doctype html>
<html>
... (truncated)

We can see the fields:

  • Protocol Version: e.g. HTTP/1.1
  • Status: 200, 404, 500
  • Response Headers: key-value pairs similar to request headers
    • Content-Type: text/html
    • Content-Language: de-DE
  • Body: Actual content (e.g. HTML), but can be anything (images, audio, JSON, etc.)

HTTP Statuses

The server includes a status code in the first line of the response to help the client know the general category of response.

While additional detail may or may not be present in the body, the status of the response will indicate if the request succeeded, failed, or perhaps if it needs to be tried again.

  • 200 - Typically 200 indicates a successful response. You will often see this written as 200 OK.
  • 3xx - Redirects. The server suggests trying a different request. These are typically sent when a page moves.
  • 4xx - Request errors. Common codes include 401 Unauthorized which suggests a password or API key is required and the common 404 Not Found indicating that the URI refers to an unknown resource.
  • 5xx - Server errors. These indicate something has gone wrong on the server itself. Unless you are the one writing code on the server, there's rarely anything you can do to mitigate a 500 error.

See MDN HTTP response status codes for details on all standard status codes.

Common Response Headers

Response headers indicate information about the response, such as the format, length, and language. They can also be used by the server to set cookies, information that persists between requests. Without cookies or similar techniques, every request from the client to the server begins with no prior context.

  • Content-Type: Will usually be present, and is an indication of what is contained in the response body. text/html would indicate the rest of the response is HTML meant to be rendered in a browser, application/json would mean that the response contains JSON that could be parsed by your code.

A Grain of Salt

HTTP statuses and headers are "suggestions", and it is completely possible for them to be wrong. It is not uncommon to see a misconfigured API sending back Content-Type: application/json but sending HTML or vice-versa. Use them as a hint, but be prepared to handle edge cases.

Body

The body of the response begins with a blank line after the headers.

The rest of the response will be in a format hopefully negotiated by the URL and request headers and noted in the response Content-Type header as described above.

Seeing HTTP In Action

You can watch a real request and response with curl, a command-line HTTP client that comes preinstalled on most systems. The -v (verbose) flag shows the full conversation:

$ curl -v https://example.com/
*   Trying 93.184.215.14:443...
* Connected to example.com (93.184.215.14) port 443
* (... TLS handshake details ...)
> GET / HTTP/1.1
> Host: example.com
> User-Agent: curl/8.7.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/html; charset=UTF-8
< Cache-Control: max-age=3600
< Content-Length: 1256
<
<!doctype html>
<html>
...
  • Lines starting with...
    • * are curl explaining what it's doing (connecting, setting up encryption).
    • > are the request curl sent.
    • < are the response headers.
  • Everything after those lines is the response body.

HTTPS

You may have noticed that most URLs today are https:// instead of http://. Most servers today use an encrypted version of the messages shown above. The actual structure is the same, but the data sent between the client and server is encrypted.

This encryption means that someone who can eavesdrop on the network traffic (the coffee shop owner, the ISP, etc.) can only see what domain someone is connecting to, but not what content they're accessing or posting.

In practice, the encryption/decryption process is mostly invisible to client and server side code.

Further Exploration

  • Internet protocols are typically defined in documents known as RFCs. RFC 9112 describes HTTP/1.1.
  • MDN HTTP has details on the entire protocol.