# HTTP Request Anatomy

> A message sent by a client to a server, consisting of a Start Line, Headers, an Empty Line, and an optional Body.

Subject: Internet | Lesson 8 of 35 | 3 min read
Interactive lesson: https://codewithanimation.in/internet/http-request-anatomy
Previous: [Client-Server Architecture](https://codewithanimation.in/internet/client-server-architecture.md) | Next: [HTTP Response Anatomy](https://codewithanimation.in/internet/http-response-anatomy.md)

## What it is

An **HTTP Request** is a message sent by a client to a server to initiate an action or request data. It is constructed from plain text serialized over a TCP connection and consists of a **Start Line** (Method + Target + Protocol), **Headers** (metadata), an **Empty Line** (`\r\n`), and an optional **Body** (the payload).

## Why it matters

Understanding the anatomy is critical for debugging APIs in the browser's Network tab. It allows you to know exactly how to craft `fetch()` calls-knowing when to put data in the URL (query parameters) versus in the Body, and how to set appropriate `Content-Type` headers.

## How it works

At the lowest level, an HTTP/1.1 request is serialized as a plain-text stream using Carriage Return and Line Feed (`\r\n`) to separate lines. The server parses this stream line-by-line. When it hits the explicit empty line, it knows the headers have finished and any remaining bytes belong to the body payload.

## Common mistake: Common mistake

Trying to attach a JSON payload (Body) to a `GET` request. Beginners often assume they can just send a body with any HTTP method. However, `GET` requests retrieve data without modifying state, and intermediate layers (proxies, caches) will ignore the body or reject the request entirely.

## Tip: Pro tip

Use the **Envelope Analogy**. The Start Line is the front address label (Who and How). The Headers are the postage stamps and metadata ('Weight: 2lbs'). The Empty Line is the physical cardboard of the box. The Body is the actual letter or item inside.

## Try it: Try it

Hover or drag the X-Ray lens over the raw HTTP request below to reveal the semantic meaning of each segment.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/internet/http-request-anatomy

## Check yourself

### Question 1

What are the three components that make up the start line of an HTTP request?

- A. Method, Request Target (Path), and Protocol Version.
- B. Method, Headers, and Body.
- C. URL, Content-Type, and Protocol.
- D. Method, Domain Name, and Port.

Correct answer: A

The start line (e.g., `GET /index.html HTTP/1.1`) defines the exact action being taken (`GET`), the specific resource path (`/index.html`), and the version of the protocol being used (`HTTP/1.1`).

- Why not B: These are the three main sections of the entire HTTP request, not just the single start line.
- Why not C: Content-Type is a header, not part of the start line.
- Why not D: The domain and port are typically handled by the DNS and the `Host` header. The start line specifically uses the URL Path/Target.

### Question 2

Why is it a bad idea to include a Body payload in a `GET` request?

- A. HTTP semantics define `GET` as retrieving data; many servers and proxies will ignore the body or reject the request.
- B. GET requests don't support headers, so the server can't parse the body.
- C. The browser will immediately crash if a GET request has a body.
- D. It makes the request too large for the network to handle.

Correct answer: A

The HTTP specification explicitly states that a payload body in a `GET` request has no defined semantics. Because of this, intermediate caches, proxies, and backend frameworks will often strip the body out or return a '400 Bad Request' error.

- Why not B: GET requests absolutely use headers!
- Why not C: The browser won't crash, though it might throw a fetch error depending on the implementation.
- Why not D: The network handles large payloads fine; the issue is protocol semantics.

### Question 3

Where do query parameters (like `?search=shoes`) belong in an HTTP request?

- A. They are appended directly to the Request Target in the start line.
- B. Inside the Request Body.
- C. In a dedicated `Query-String` header.
- D. They are sent automatically in a separate pre-flight request.

Correct answer: A

Query parameters are an extension of the URL path. In the raw HTTP request, they are simply appended to the target path in the very first line (e.g., `GET /api/items?search=shoes HTTP/1.1`).

- Why not B: The body is reserved for larger data payloads like JSON or file uploads, not query strings.
- Why not C: There is no standard HTTP header for query parameters.
- Why not D: Pre-flight (OPTIONS) requests are used to check CORS permissions, not to send query data.

## Remember this

- Start Line contains the Method, Target (URL path/query), and Protocol Version.
- Headers are key-value pairs providing metadata about the request.
- The empty line explicitly separates the Headers from the Body.
- Never send a Body with a GET request-use query parameters in the URL instead.
