HTTP Responses
An HTTP response consists of multiple lines, each terminated by a carriage return character (CR) and line feed character (LF). It begins with a status line, giving the version of HTTP that was used, a three-digit status code to indicate success or failure, and an optional phrase to explain the status code.
A status code of 200 indicates that the request was handled successfully. Codes beginning with a 4 indicate an error in the request made by the client. Codes beginning with a 5 indicate an error on the server side.
After the status line are zero or more lines of header, each specifying a separate header field. A header field consists of the field’s name (case-insensitive), a colon, and the field’s value.
The third element is an empty line, i.e., a line that contains only the CR and LF characters.
The final element is the message body. This contains the content of the response. Everything prior to the message body is text, but the message body can be text (e.g., an HTML document or a stylesheet) or binary data (e.g., an image or a video file).
Task 3.3
-
Open a terminal window. In that terminal, follow the installation instructions on the uv website to install the uv project manager tool.
-
Create a directory in which to do this task,
cdinto it, then enteruv init -
Install the httpx library and command line interface with
uv add 'httpx[cli]'This will create a virtual environment containing the software. Activate this environment now by entering
source .venv/bin/activateYou can deactivate this environment at any time simply by entering
deactivate, but don’t do this yet! -
You should now be able to use httpx on the command line to make a GET request to example.com:
httpx https://www.example.comThe httpx tool will display the entirety of the HTTP response issued by the example.com web server. Take a look at the first line:
HTTP/1.1 200 OKThis identifies the version of the protocol used by client & server, and provides a status code and message. A status code of 200 indicates that the request succeeded.
The next eleven lines are the header of the response. Take a look at each of these lines in turn. Use the list of response header fields to help you understand what each of them represents.
Note, in particular,
Content-Type, set totext/html, indicating an HTML documentContent-Encoding, set togzip, indicating that content has been compressedContent-Length, set to 363 bytes (the compressed size of the content)Connection, set tokeep-alive
That
keep-alivevalue indicates that the server is keeping the TCP connection active, allowing it to be reused by subsequent GET requests that might be needed to retrieve further resources such as stylesheets or images. Without this, a separate TCP connection would be made for every one of these requests, slowing down your web browsing considerably!After the last line of the header is a single empty line, followed by the content itself. You can see that this is HTML.
-
Now repeat the request, but this time ask for more verbose output by adding the
-voption to the end of the command:httpx https://www.example.com -vNow you will see additional output displayed before the details of the response.
The lines beginning with
*appear here because the connection to the server is protected by Transport Layer Security (thehttpsat the start of the URL tells you that this is happening). If you look at these lines, you’ll see details of the cipher and hash function used to secure the connection, plus information about the TLS certificate that the server uses to authenticate itself to clients. -
After the lines dealing with TLS, you should see details of the request, starting with
GET / HTTP/1.1which indicates a GET request for the site’s home page, using version 1.1 of HTTP.
After this comes the request header. Study each of these five lines, using the list of request header fields to help you understand their purpose.