Learn HTTPX

Modern HTTP client for Python with sync and async APIs and HTTP/2

15 lessons · 3 hours · 4 levels · covers HTTPX 0.28

HTTPX is a fully featured HTTP client for Python that offers both a synchronous API close to Requests and an async API for asyncio and Trio. It adds strict timeouts by default, HTTP/2 support, pluggable transports and the ability to call ASGI and WSGI apps directly, which makes it popular for modern services and for testing web frameworks such as FastAPI.

Before you start

HTTPX builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics4 lessons · 34 min in total
2. PracticalUse it productively at work, with the usual idioms8 lessons · 1 hour in total
3. AdvancedThe less common features, performance and trade-offs12 lessons · 2 hours in total
4. ExpertInternals and the edge cases experts know15 lessons · 3 hours in total

The HTTPX tree

HTTPXYour first request with HTTPXYour first requestwith HTTPXWorking with responsesWorking withresponsesParams, headers and request bodiesParams, headers andrequest bodiesTimeouts and redirects by defaultTimeouts andredirects by defaultUsing httpx.ClientUsing httpx.ClientException hierarchyException hierarchyAsyncClient and concurrent requestsAsyncClient andconcurrent requestsTesting with MockTransportTesting withMockTransportModules and importsModules and importsContext managers and withContext managersand withExceptions in depthExceptions in depthasync and await with asyncioasync and await withasyncioTesting with pytest and unittestTesting with pytestand unittestIterators and generatorsIterators andgeneratorsAdvanced generators and itertoolsAdvanced generatorsand itertools
Essentials and Practical shown (8 of 15 lessons); the full list is below. Click a lesson to read it.

All 15 lessons, in order

Making requests

  1. Your first request with HTTPX · Essentials · 7 min

    Install HTTPX and send GET and POST requests with the top-level functions, reading status and JSON from the response.

  2. Working with responses · Essentials · 9 min

    Read text, bytes and JSON from an httpx.Response, use status helpers, and turn error statuses into exceptions.

  3. Params, headers and request bodies · Essentials · 10 min

    Send query parameters, headers, JSON, form data, files and raw bytes using params=, headers=, json=, data=, files= and content=.

  4. Timeouts and redirects by default · Essentials · 8 min

    Know HTTPX's safer defaults: a 5-second timeout on everything and redirects that are not followed unless you ask.

Clients and errors

  1. Using httpx.Client · Practical · 10 min

    Create a Client to reuse connections and share base_url, headers, auth, timeouts and redirect settings across requests.

  2. Exception hierarchy · Practical · 9 min

    Handle HTTPX's exceptions: RequestError for transport problems, HTTPStatusError for error responses, and their subclasses.

  3. Testing with MockTransport · Practical · 11 min

    Replace the network with httpx.MockTransport so tests call a Python handler function instead of a real server.

Async

  1. AsyncClient and concurrent requests · Practical · 12 min

    Use httpx.AsyncClient with async/await to run many requests concurrently with asyncio.gather and a semaphore.

Streaming and configuration

  1. Streaming responses and uploads · Advanced · 11 min

    Stream large or long-lived responses with client.stream and iter_bytes, and stream request bodies from generators.

  2. Fine-grained timeouts, pool limits and retries · Advanced · 11 min

    Tune connect/read/write/pool timeouts, connection pool Limits and transport-level connect retries.

  3. Custom auth flows and event hooks · Advanced · 12 min

    Write httpx.Auth classes with generator-based auth_flow for token refresh or signing, and use event hooks for logging.

  4. HTTP/2, proxies and TLS · Advanced · 11 min

    Enable HTTP/2, route traffic through proxies with proxy= and mounts=, and configure certificate verification with SSL contexts.

Transports and internals

  1. Calling ASGI and WSGI apps directly · Expert · 12 min

    Use ASGITransport and WSGITransport to send real HTTP requests into a Python web app in-process, without a server.

  2. Writing custom transports · Expert · 13 min

    Implement BaseTransport.handle_request to add retries, caching or routing beneath the client, wrapping the default transport.

  3. Internals: httpcore and AnyIO · Expert · 12 min

    Understand how HTTPX layers over httpcore's connection pools, how async backends are chosen, and where performance goes.