build-an-http-server / lesson-14.md
Lesson 14 · Parsing requests

Assemble the Request struct

You have parsed the request line, the headers, and the body separately - today you gather them into one Request value, the single object every handler will receive.

The goal

Read a full request off a stream and return one Request holding its method, target, version, headers, and body.

Start here - the target
TO DO
Scenario: Parsing a complete request
Giventhe request "POST /submit HTTP/1.1\r\nHost: example.com\r\nContent-Length: 2\r\n\r\nhi"
Whenit is parsed
Thenmethod = "POST", target = "/submit", version = "HTTP/1.1"
Andthe Host header is "example.com" and the body is "hi"
Background

Every piece you have built so far — the head reader, the request-line parser, the header collector, the Content-Length body reader — now snaps together into one function that turns a raw stream into a Request. This struct is the shape every handler in the rest of the project speaks; nothing downstream touches raw bytes again.

The order is fixed by the wire format: read the head, split off the request line, fold the remaining lines into headers, then read the body its Content-Length describes. One value comes out carrying everything the request said.

Make it work
type Request struct {
Method, Target, Version string
Headers Headers
Body []byte
}
// parseRequest: readHead -> parse request line -> collect headers -> read body
CheckpointDONE
A raw stream parses into a complete Request value. Commit.