Asynchronous retrieval
Retrieve a web resource from an async application.
HTTPX · Function
AsyncClient.get asynchronously performs an HTTP GET request and returns an HTTPX response. Await the call, and manage the client with an async context manager or explicit closure.
AsyncClient.get performs an asynchronous HTTP GET and returns a response after the request completes. Call it with await, typically inside an async with block so the client’s resources are closed automatically.
await client.get(url, *, params=None, headers=None, cookies=None, auth=None, proxy=None, follow_redirects=False, verify=True, timeout=Timeout(timeout=5.0), trust_env=True)| Name | Type | Description |
|---|---|---|
url | str or URL | Target URL for the GET request. |
params | QueryParamTypes | None | Optional query parameters appended to the URL. |
headers | HeaderTypes | None | Optional request headers. |
cookies | CookieTypes | None | Optional cookies sent with the request. |
auth | AuthTypes | None | Optional authentication configuration. |
proxy | ProxyTypes | None | Optional proxy configuration. |
follow_redirects | bool | Whether redirects are followed. |
verify | ssl.SSLContext | str | bool | TLS certificate verification setting. |
timeout | TimeoutTypes | None | Request timeout configuration. |
trust_env | bool | Whether environment configuration is trusted. |
Response
client.get and stores the resulting response.await leaves you with a coroutine rather than the completed response.await client.aclose().import asyncio
import httpx
async def main():
async with httpx.AsyncClient() as client:
response = await client.get(
"https://httpbin.org/get",
params={"q": "httpx"},
)
response.raise_for_status()
print(response.json()["args"])
asyncio.run(main())Creates a scoped asynchronous client, awaits a GET request with query parameters, validates success, and reads the decoded JSON response.
Retrieve a web resource from an async application.
Send filters or search terms without manually constructing a URL.
Reuse one client for related concurrent operations.
AsyncClient.get do?AsyncClient.get sends an HTTP GET request and must be awaited. It returns an HTTPX response object, which exposes status, headers, body, and related response information.
Follow-up: How should a client be managed across multiple requests? Use one shared AsyncClient when appropriate, and close it with the async context manager or await client.aclose().
The method is invoked on httpx.AsyncClient, and its URL may include request-specific options such as query parameters. The documented async usage is response = await client.get(...).
Follow-up: How would you ensure a failed HTTP status is handled? Inspect the returned response status or call the response’s status-checking method before consuming the result.
AsyncClient instead of the synchronous top-level helper?An AsyncClient provides asynchronous HTTP access together with connection pooling and can be shared between tasks. A context-managed instance ensures its resources are released after use.
Follow-up: What client feature can be enabled for HTTP/2? Construct AsyncClient with http2=True, then inspect the response protocol version through its HTTP-version property.
AsyncClientAsyncClient.requestAsyncClient.streamResponsehttpx.get