We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Body Review

Take a look at the struct we've been parsing requests into:

type Request struct {
	RequestLine RequestLine
	Headers     headers.Headers
	Body        []byte
    // ...
}

Now compare it to the official Golang http.Request struct:

type Request struct {
    Proto    string
    Method   string
    URL      *url.URL
    Header   http.Header
    Body     io.ReadCloser
    // ...
}

There are a few differences:

  • We have a RequestLine struct, while they've elevated the Method, Target, and Version into their own top-level fields. It's basically the same data, but the layout is a bit different.
  • We have a Headers struct, they named theirs Header. Again, same data, slightly different layout and naming. I prefer to call them "headers" because that's usually how web developers refer to them, the Go stdlib refers to the entire collection of field lines as a "header"... but I digress.
  • The most important difference: we have a Body field that just stores the entire body as a []byte in memory, while the Go stdlib has an io.ReadCloser interface. Our solution is perhaps simpler, but it's more limiting to the users of our library... we force them to load the entire body into memory, before interacting with it. The Go stdlib allows the user to read the body in chunks, or even stream it to a file, which is often necessary for large bodies. More on this later.