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

Content Types

An important part of the HTTP request-response cycle is the negotiation of content types.

A web client signals the content types it can handle by including the Accept header in a request. Types are specified in a standard way, with a type/subtype format. Wildcards can be used in either part of this format to represent ‘any type’.

For example,

Accept: text/html

indicates that the client is prepared to accept HTML documents, whereas

Accept: text/*

indicates that the client can handle any kind of text document, and

Accept: */*

indicates its willingness to handle any type of content.

Given the hints provided by a client, the server can make an appropriate choice of which type of content it delivers in response.

Here are some examples of common content type specifications:

TypeDescription
application/jsonData in the JSON format
application/pdfPDF document
image/jpegJPEG image
image/pngPNG image
image/svg+xmlScalable Vector Graphics, in XML format
text/cssCascading Style Sheet
text/csvData in the CSV format
text/htmlHTML document
text/plainPlain text
text/xmlXML document
video/quicktimeQuicktime movie file

Task 3.4

  1. Return to the directory that you created in Task 3.3—the one in which you installed httpx using the uv project manager tool. In this directory, activate the virtual environment with

    source .venv/bin/activate
    

    (or the Windows equivalent if you are using that OS)

  2. Make the following GET request using httpx:

    httpx https://httpbin.io/html
    

    Notice the Content-Type field in the response header, set to text/html. You can also see that the content is clearly an HTML document.

  3. Now make a GET request for something different:

    httpx https://httpbin.io/image
    

    Notice that the server responds with 415 UNSUPPORTED MEDIA TYPE, indicating that it did not receive enough information from the client about acceptable media types, and it wasn’t willing to choose a default media type from the options available.

  4. Now try the following:

    httpx https://httpbin.io/image --headers Accept image/png
    

    This adds Accept: image/png to the header of the GET request. You should see a 200 OK response from the server.

    The httpx tool won’t attempt to display the image because you are running the command in a terminal. Instead, it will show you the size of the content that was included with the response. You can save this content to a file by adding this option to the end of the command:

    --download test.png
    

    Try this out and look at the image that is downloaded.

  5. Repeat the above using image/jpeg as the accepted media type. You should see that a different image is downloaded.

  6. Finally, try making the following GET request:

    httpx https://httpbin.io/json
    

    Notice that the response has a content type of application/json, and that it clearly contains JSON data rather than HTML or some other kind of content.

    This is typical of how a native app on a mobile device might interact with a server. The job of the server in such cases is not to deliver the UI to a web browser; instead, the app provides the UI and the server provides the data that the app will display in that UI.