httpx
Fully featured next-generation HTTP client for Python 3. Provides both synchronous and asynchronous APIs with HTTP/1.1 and HTTP/2 support. Current version is 0.28.1 (December 2024). Pre-1.0: minor version bumps may introduce breaking changes.
Warnings
- breaking proxies= argument was removed in 0.28.0. Using it raises TypeError.
- breaking app= shortcut argument was removed in 0.28.0. It was deprecated in 0.27.0.
- breaking Redirects are NOT followed by default (changed in 0.20.0). Requests that previously auto-redirected now return the 3xx response directly.
- deprecated verify='path/to/ca-bundle' (string) and cert=('cert', 'key') arguments are deprecated in 0.28.0 and will raise DeprecationWarning.
- gotcha httpx is pre-1.0. Minor version bumps (e.g. 0.27 → 0.28) can and do introduce breaking changes. The maintainers recommend pinning the minor version.
- gotcha HTTP/2 is disabled by default. Installing httpx alone does not enable it.
- gotcha Top-level functions (httpx.get, httpx.post, etc.) open a new TCP connection on every call. For more than one request to the same host this is inefficient.
Install
-
pip install httpx -
pip install 'httpx[http2]' -
pip install 'httpx[cli]' -
pip install 'httpx[brotli]' -
pip install 'httpx[zstd]'
Imports
- httpx
import httpx
- httpx.Client
with httpx.Client() as client: r = client.get('https://example.com') - httpx.AsyncClient
async with httpx.AsyncClient() as client: r = await client.get('https://example.com') - httpx.Timeout
httpx.Client(timeout=httpx.Timeout(5.0, connect=2.0))
- httpx.BasicAuth / httpx.Auth
httpx.Client(auth=('user', 'pass')) # or httpx.Client(auth=httpx.BasicAuth('user', 'pass'))
Quickstart
import httpx
# One-off request (new connection each time — fine for scripts)
r = httpx.get('https://httpbin.org/get', timeout=10.0)
r.raise_for_status()
print(r.status_code) # 200
print(r.json()) # parsed JSON body
# Recommended: reuse a Client for multiple requests (connection pooling)
with httpx.Client(base_url='https://httpbin.org', timeout=10.0) as client:
resp = client.get('/get', params={'key': 'value'})
resp.raise_for_status()
print(resp.json())
# Async variant
import asyncio
async def main():
async with httpx.AsyncClient(base_url='https://httpbin.org', timeout=10.0) as client:
resp = await client.get('/get')
resp.raise_for_status()
print(resp.json())
asyncio.run(main())