Details

Language / Topic
pythonPython
Category
Libraries
Source
encode

Rules

balanced
- Use `httpx.AsyncClient()` with `async with` for async HTTP — it supports HTTP/2 and connection pooling out of the box.
- Use `client.get()`, `client.post()` with `json=`, `params=`, `headers=` kwargs — HTTPX API mirrors Requests for easy migration.
- Set `timeout=httpx.Timeout(10.0, connect=5.0)` explicitly — HTTPX defaults to 5s but production APIs may need longer read timeouts.
- Set `base_url` and `timeout` on the client instance for shared configuration.
- Use `response.raise_for_status()` to raise on HTTP errors. Handle `httpx.HTTPStatusError`.
- Use `client.stream()` for large response bodies without loading into memory.
- Use `httpx.AsyncClient(transport=httpx.MockTransport(handler))` for testing without network calls.