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:
| Type | Description |
|---|---|
application/json | Data in the JSON format |
application/pdf | PDF document |
image/jpeg | JPEG image |
image/png | PNG image |
image/svg+xml | Scalable Vector Graphics, in XML format |
text/css | Cascading Style Sheet |
text/csv | Data in the CSV format |
text/html | HTML document |
text/plain | Plain text |
text/xml | XML document |
video/quicktime | Quicktime movie file |
Task 3.4
-
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)
-
Make the following GET request using httpx:
httpx https://httpbin.io/htmlNotice the
Content-Typefield in the response header, set totext/html. You can also see that the content is clearly an HTML document. -
Now make a GET request for something different:
httpx https://httpbin.io/imageNotice 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. -
Now try the following:
httpx https://httpbin.io/image --headers Accept image/pngThis adds
Accept: image/pngto the header of the GET request. You should see a200 OKresponse 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.pngTry this out and look at the image that is downloaded.
-
Repeat the above using
image/jpegas the accepted media type. You should see that a different image is downloaded. -
Finally, try making the following GET request:
httpx https://httpbin.io/jsonNotice 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.