Deferred sending
Prepare a request before choosing when or through which client it will be transmitted.
HTTPX · Class
httpx.Request is HTTPX’s low-level model for constructing an HTTP request before transmission. It is useful when request preparation and sending need to be separate.
httpx.Request is a low-level request model: it stores the method, URL, headers, cookies, query parameters, body, and optional extensions. Build it when you need to prepare a request separately from transmission, then pass it to a client’s send() method; the request is sent as-is rather than being modified by the client.
httpx.Request(method, url, *, content=None, data=None, files=None, json=None, params=None, headers=None, cookies=None, auth=None, extensions=None)| Name | Type | Description |
|---|---|---|
method | str | HTTP method, such as GET, POST, PUT, PATCH, DELETE, OPTIONS, or HEAD. |
url | URL or str | Destination URL. |
content | bytes, str, or iterable of bytes | Raw request body content. |
data | Mapping or iterable | Form data. |
files | Mapping or iterable | Multipart upload files. |
json | Any | JSON-serializable request body. |
params | Mapping or iterable of two-tuples | Query parameters. |
headers | Mapping or iterable | HTTP headers. |
cookies | Mapping or iterable | Cookies. |
auth | Auth object or tuple | Authentication configuration. |
extensions | Mapping | Request extensions. |
A Request object representing the constructed HTTP request.
import asyncio
import httpx
async def main():
request = httpx.Request("GET", "https://example.org")
async with httpx.AsyncClient() as client:
response = await client.send(request)
print(response.status_code)
asyncio.run(main())Creates a request explicitly, then sends it through an asynchronous client.
Prepare a request before choosing when or through which client it will be transmitted.
Specify the exact method, URL, headers, query parameters, and body before dispatch.
Pass a prepared request to synchronous or asynchronous client sending APIs.
httpx.Request represents a prepared HTTP request, while Client.send() or AsyncClient.send() performs transmission. This separation lets code construct a request first and send it later.
Follow-up: How would you send one request asynchronously? Construct it with httpx.Request, create an AsyncClient, and await client.send(request).
The request is transmitted as-is and unmodified. Therefore, values that must be present in the outgoing request should be supplied when the Request is constructed.
Follow-up: How can client-wide behavior be applied instead? Use a client’s configuration or event hooks for behavior that belongs to the client layer, rather than assuming Request will add it.
The constructor accepts method, URL, content, form data, files, JSON, query parameters, headers, cookies, authentication, and extensions according to the documented signature. However, several client-level controls are not constructor parameters.
Follow-up: Which controls are excluded? The documentation specifically excludes auth, timeout, follow_redirects, mounts, verify, and cert from httpx.Request().
httpx.RequestClient.send()AsyncClient.send()httpx.request()Response