HTTP Request Structure

Learn the four essential parts of every HTTP request - method, path, headers, and body - and how they work together to communicate with a web server.

When a client (like your web browser or a mobile app) wants something from a server, it sends an HTTP request. This request is a structured message that tells the server exactly what the client wants and includes any necessary details. Understanding this structure is the foundation for building any web application with Axum, because Axum uses these parts to decide which code should run and what data that code receives.

An HTTP request is made of up to four main pieces:

  1. Request line - contains the method, path, and HTTP version.
  2. Headers - key-value pairs with metadata about the request.
  3. Blank line - separates headers from the body.
  4. Body - optional data sent to the server (used mainly with POST, PUT, PATCH).

We will explore each of these in detail, starting with a simple example and gradually building up to full, realistic requests.

The Request Line

The request line is the first line of every HTTP request. It tells the server three things in a fixed order:

METHOD  PATH  HTTP-VERSION

For example:

GET /index.html HTTP/1.1
  • METHOD - the action the client wants to perform. Common methods are GET, POST, PUT, PATCH, and DELETE. We have a dedicated page on HTTP methods later, but for now just know that the method is a word that signals the intent.
  • PATH - the resource on the server that the client wants to access. It starts with a forward slash (/) and identifies a specific route or file. The path can also contain a query string (like /search?q=rust), but the query string is technically not part of the path itself; it is added after a ? and is sent in the request line as part of the target.
  • HTTP-VERSION - the version of the HTTP protocol the client is using. For most modern applications, this is HTTP/1.1 or HTTP/2. Axum supports both, but the raw text format we discuss here is from HTTP/1.1. HTTP/2 uses a binary format (frames instead of readable text lines) but the logical structure - method, path, headers, body - remains the same. HTTP/2 support in axum::serve is off by default: it needs the http2 feature enabled (for example, axum = { version = "0.8", features = ["http2"] }).

A few more examples of request lines:

POST /api/users HTTP/1.1
PUT /api/users/42 HTTP/1.1
DELETE /api/users/42 HTTP/1.1

Notice that the method is always uppercase by convention, though the HTTP standard says it is case-sensitive and should be treated as such. Browsers and libraries always send uppercase methods. If you write code that manually sends a request, you must use the correct case or the server may reject it.

Path vs. URL:

The path is only one part of a full URL. A URL like https://example.com/docs/intro?page=2 contains:

  • Scheme: https
  • Host: example.com
  • Path: /docs/intro
  • Query string: page=2

In the HTTP request line, the client sends only the path and the query string (if any), not the scheme or host. The host is sent separately in a header (covered next).

HTTP Headers

After the request line, the client sends one or more headers. Each header is a key-value pair that provides additional information about the request or the client itself. Headers are written one per line in the format:

Header-Name: value

Header names are case-insensitive, so Host, host, and HOST all mean the same thing. By convention, they are written in title case (first letter of each word capitalized). The value can be any text, and some headers can appear multiple times with different values.

Here is a request line followed by a few common headers:

GET / HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)
Accept: text/html,application/xhtml+xml
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
  • Host - specifies the domain name of the server. This is the only header required in HTTP/1.1. It allows a single server to host multiple websites (virtual hosting).
  • User-Agent - identifies the client software (browser, app, or library) making the request. Servers often use this to tailor content or log analytics.
  • Accept - tells the server what media types the client can understand (like text/html, application/json).
  • Accept-Language - indicates the client's preferred language(s).
  • Connection - controls whether the network connection stays open for further requests.

There are dozens of standard headers, and you can also define custom headers using a clear, descriptive name (often with a vendor or application prefix, like Acme-Request-Id). The old habit of prefixing every custom header with X- was deprecated by RFC 6648 in 2012, so new headers should not use the X- prefix. In Axum, headers are accessible through extractors and middleware, but we'll focus on the raw structure now.

Two headers are especially important when a request has a body:

  • Content-Type - tells the server what format the body data is in (e.g., application/json, text/plain, application/x-www-form-urlencoded).
  • Content-Length - indicates the size of the body in bytes. This helps the server know when it has received the complete body.

Headers Are Just Metadata:

Headers never contain the actual resource being requested; they only describe the request or the client. Think of them as the outside of an envelope: the address, return address, and special handling instructions, while the body (if any) is the letter inside.

The Request Body

Not all requests have a body. In general:

  • GET and DELETE requests usually do not have a body.
  • POST, PUT, and PATCH requests usually do have a body, because they are used to send data to the server (like form submissions, JSON payloads, or file uploads).

The body is separated from the headers by a blank line (a line containing only CRLF, i.e., \r\n). After that blank line, the raw bytes of the body are sent. The body can be in any format, but the server needs to know how to interpret it, which is why the Content-Type header is so important.

Here's an example of a complete POST request with a JSON body:

POST /api/users HTTP/1.1
Host: api.example.com
Content-Type: application/json
Content-Length: 47
{"name": "Alice", "email": "alice@example.com"}

Let's break down that example:

  • The request line says: POST /api/users HTTP/1.1 - the client wants to create a new user.
  • The headers include Content-Type: application/json - telling the server the body is JSON.
  • Content-Length: 47 - the body is exactly 47 bytes long.
  • After a blank line, the JSON data follows.

The blank line is critical. If it is missing, the server will treat the first line of the body as an extra header and the request will likely fail or behave unexpectedly.

Don't Forget the Blank Line:

When manually constructing an HTTP request (for testing or learning), the most common mistake is forgetting the blank line between headers and body. Without it, the server cannot tell where headers end and where the body begins. Always ensure there is a blank line (two CRLF sequences in a row) before writing the body.

A Complete Request from Top to Bottom

Let's examine a realistic HTTP request exactly as it would be sent over the network. We'll use a GET request to fetch the homepage of a website:

GET / HTTP/1.1
Host: www.example.com
User-Agent: curl/8.0.1
Accept: */*

Notice that this request has no body because it's a GET. The blank line at the end signals the end of the headers.

Compare that with a POST request that submits a login form:

POST /login HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 30
username=alice&password=secret

In this case, the body is form data (key-value pairs separated by &). The Content-Type tells the server how to parse it.

You're Now Reading HTTP Raw Text:

If you can look at the above examples and identify the method, path, headers, and body, you already understand the structure of an HTTP request. This skill helps when debugging APIs, writing tests, or using tools like curl and telnet.

Common Mistakes and Misunderstandings

1. Using the Wrong Case for the Method

HTTP method names are case-sensitive. GET is correct, but get or Get will be treated as an unknown method. Most servers (including Axum) will return a 405 Method Not Allowed error if they receive an unexpected method.

2. Forgetting the Host Header in HTTP/1.1

In HTTP/1.1, the Host header is mandatory. If you send a request without it, the server will respond with a 400 Bad Request error. This is why every HTTP client library automatically includes it.

3. Incorrect Content-Length

If you include a body, the Content-Length header must exactly match the number of bytes in the body. If it's too small, the server may truncate the body; if it's too large, the server may wait for more data that never arrives, causing timeouts. Fortunately, most libraries calculate this automatically.

4. Mixing Up Query String and Body

New developers sometimes put data in the query string that should go in the body, or vice versa. As a rule of thumb:

  • Use the query string for filtering, sorting, and pagination (data that doesn't change the server resource).
  • Use the request body for creating or updating resources (data that becomes part of the new resource).

For example:

  • GET /users?page=2&limit=50 - query string is appropriate.
  • POST /users with body {"name": "Alice"} - body is appropriate.

5. Ignoring the Content-Type Header

If you send a JSON body but omit the Content-Type header, the server won't know how to parse it. Many frameworks will default to text/plain, causing errors when your code tries to deserialize it as JSON. Always set the correct Content-Type.

Missing Content-Type Causes Silent Failures:

A common bug occurs when a developer sends a JSON body but forgets to set Content-Type: application/json. The server might accept the request and then fail to parse the body, returning a generic 400 Bad Request or 415 Unsupported Media Type. If you're debugging a request that should have a body but the server says it can't read it, check the Content-Type header first.

How Axum Sees the Request

So far we have described the raw HTTP request as text. When you build a web server with Axum, you don't have to parse that raw text yourself; Axum (via the underlying hyper library) does it for you and presents the request as typed data. But the concepts we covered map directly to Axum's routing and extraction system:

  • Method and path are used by Router to match a request to a handler function. For example, a route like .route("/users", get(handler)) only runs when the request line is GET /users HTTP/1.1.
  • Headers are available in handler extractors (like HeaderMap) or through middleware. You can read specific headers or iterate over all of them.
  • Body is consumed by extractors like Json, Form, or Bytes. The extractor checks the Content-Type and parses the body accordingly.

This separation means you can focus on application logic instead of string parsing, but understanding the underlying structure helps you debug issues and write better tests.

Not All Bodies Are Consumed Automatically:

In Axum, you decide which extractor to use for the body. If you don't use a body extractor, the body is simply ignored (but the connection may be reused). If you use Json, the body is automatically read and deserialized. If you need raw bytes, you can use Bytes. This gives you fine-grained control.

Beyond HTTP/1.1: HTTP/2, HTTP/3, and TLS

Everything above shows HTTP/1.1, the version where requests travel as readable text. Two newer versions change how the bytes travel, not what a request means:

  • HTTP/2 keeps the same methods, paths, headers, and bodies, but sends them as binary frames multiplexed over a single connection instead of plain text. You cannot read an HTTP/2 request by eye the way you can read the examples on this page. In Axum, HTTP/2 works through axum::serve only when the http2 feature is enabled; the http1 feature (on by default) covers HTTP/1.1.
  • HTTP/3 keeps the same HTTP semantics again, but runs over QUIC (a UDP-based transport) instead of TCP. Axum does not serve HTTP/3 directly through axum::serve; it arrives through a proxy or a dedicated server setup.
  • TLS (HTTPS) encrypts the whole conversation. The request structure is unchanged - TLS wraps the bytes, it does not change the method, path, headers, or body inside. axum::serve serves plain HTTP only; in production, TLS is usually terminated by a reverse proxy in front of your Axum server, or you drive Hyper yourself with a TLS acceptor.

The logical structure you learned on this page - request line, headers, blank line, body - is the same in all three versions. Only the transport underneath changes.

Wrapping Up

You now know the anatomy of an HTTP request:

  • The request line carries the method, path, and HTTP version.
  • Headers provide metadata as key-value pairs.
  • The body carries optional data, separated from headers by a blank line.

These three parts work together to let a client communicate its intention to a server. Every web framework, including Axum, is built on top of this simple structure. When you write your first Axum route, you'll be telling the framework: "When a request comes in with this method and this path, run this code." The headers and body then let that code access the finer details of the request.

Understanding HTTP request structure is not just academic; it empowers you to debug network issues, read server logs, and construct requests manually with tools like curl or telnet. It's a foundational skill that will serve you throughout your journey with Axum and web development in general.