Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

  1. Open a terminal window. In that terminal, follow the installation instructions on the uv website to install the uv project manager tool.

  2. Create a directory in which to do this task, cd into it, then enter

    uv init
    
  3. 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/activate
    

    You can deactivate this environment at any time simply by entering deactivate, but don’t do this yet!

    Note

    The activation and deactivation commands given above are for Linux and macOS systems.

    If you are doing this on a machine running Microsoft Windows, you should be able to use the batch files activate.bat and deactivate.bat instead. These should be located in the .venv\Scripts directory.

  4. You should now be able to use httpx on the command line to make a GET request to example.com:

    httpx https://www.example.com
    

    The 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 OK
    

    This 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 to text/html, indicating an HTML document
    • Content-Encoding, set to gzip, indicating that content has been compressed
    • Content-Length, set to 363 bytes (the compressed size of the content)
    • Connection, set to keep-alive

    That keep-alive value 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.

  5. Now repeat the request, but this time ask for more verbose output by adding the -v option to the end of the command:

    httpx https://www.example.com -v
    

    Now 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 (the https at 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.

  6. After the lines dealing with TLS, you should see details of the request, starting with

    GET / HTTP/1.1
    

    which 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.