Sub topic 8
Sending the HTTP request and reading the response
This chapter assumes you know what a URL, an IP address and a TCP connection are, and that the TLS handshake from the previous chapter has finished. The browser now holds a secure channel to the server. It can send the actual question: "give me this page."
The request
HTTP is a text-based request and response protocol. In HTTP/1.1 the browser writes a request as plain text (before TLS encrypts it). For the URL https://example.com/docs/intro?lang=en, the request looks like this:
GET /docs/intro?lang=en HTTP/1.1
Host: example.com
Accept: text/html
User-Agent: example-client
Connection: keep-aliveThe first line is the request line. It has three parts:
- The method, here
GET, says what to do.GETreads a resource.POSTsends data to be processed. Typing a URL always produces aGET. - The target is the path plus the query string. The scheme, host and port are not repeated here, because the connection already points at the right server.
- The protocol version tells the server which dialect of HTTP to speak.
The lines after it are headers, written as Name: value. Host is required in HTTP/1.1. One server and one IP address can host many sites, and Host tells the server which one you want. Accept lists the content types the browser can handle. The browser also adds headers you did not type, such as cookies for that host, language preferences and caching validators.
The response
The server replies with a status line, headers, a blank line and then the body:
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=3600
Content-Length: 1256
<!doctype html>
<html>...</html>- The status line holds the version, a numeric status code and a short reason phrase.
200means success. Other classes, such as301redirects and404errors, are covered in the next chapter. Content-Typetells the browser how to interpret the body. The browser relies on this header, not on the file name in the URL.charset=utf-8says how to decode the bytes into text.Cache-Controlsays how long the response may be reused without asking again.Content-Lengthgives the body size in bytes, so the browser knows when the body ends.
The blank line is the boundary. Everything before it is metadata, and everything after it is the body.
Common mistakes
- Reading
Content-Lengthas the size on screen. It is the size of the body as sent, which may be compressed. CheckContent-Encodingtoo. - Expecting the body to arrive all at once. The browser can start parsing the first bytes while the rest is still arriving.
- Forgetting that headers are case-insensitive but values such as paths are not.
- Assuming a
200means the page is complete. It only means this one response succeeded. The HTML will usually trigger more requests.
What happens next
The browser looks at Content-Type. For text/html it hands the bytes to the HTML parser, which starts building the DOM. That is the subject of a later chapter.
Exercise
A response has the header Content-Type: text/html; charset=utf-8, but the URL ends in /report.pdf. How does the browser treat the body, and why?
Show answer
It treats the body as HTML. The browser decides how to interpret a response from the Content-Type header, not from the file extension in the URL.
Exercise
Why is the Host header required in HTTP/1.1 even though the browser has already opened a connection to the server's IP address?
Show answer
One IP address can serve many different sites. The IP address only identifies the machine, so Host tells the server which site the request is for.