HTTPX is a “next-generation” HTTP client for Python 3. Its goal is to retain the user-friendly and understandable API of requests while adding modern features such as asynchronous support and HTTP/2. In other words, if you like the syntax of requests but want async/await, HTTP/2, or stricter timeout controls, HTTPX is the familiar yet fresh choice.
In a nutshell, its features can be summarized as: compatible with requests style, supports sync/async, supports HTTP/1.1 & HTTP/2, built-in CLI, complete type annotations, and high test coverage.
What Pain Points Does It Address (Why Use It)
- • Want to migrate from synchronous to asynchronous but don’t want to learn a bunch of new APIs? HTTPX provides both synchronous and asynchronous interfaces, making migration cost-effective.
- • Need the performance advantages of HTTP/2 (especially in scenarios with many concurrent connections or long-lived connections)? Requests does not natively support it, but HTTPX does.
- • Have strict requirements for connection pools, Keep-Alive, timeout controls, cookies, proxies, etc.? HTTPX’s design is more modern, with clearer timeout rules.
- • Want to send requests directly to WSGI/ASGI applications for testing without running a real server? HTTPX supports direct calls to WSGI/ASGI, making unit testing very convenient.
- • Want to send requests for debugging directly from the command line? It comes with an optional CLI, which is quite handy.
In short: more comprehensive features, clearer boundaries, and friendlier support for concurrency, but the syntax won’t scare you away.
Installation — Quick Start (Two Common Use Cases) Basic installation (recommended Python 3.9+):
- • pip install httpx
If you need HTTP/2 support:
- • pip install “httpx[http2]”
If you want to use the command line client:
- • pip install “httpx[cli]”
Note: HTTPX relies on httpcore, and HTTP/2, socks, brotli, etc. are optional extensions that can be installed as needed.
Quick Example — Synchronous & Asynchronous Practice
Synchronous request (similar to requests):
- • import httpx
- • r = httpx.get(‘https://www.example.org/’)
- • print(r.status_code, r.headers[‘content-type’])
- • print(r.text[:200])
Asynchronous request (async/await):
- • import httpx, asyncio
- • async def main(): async with httpx.AsyncClient() as client: r = await client.get(‘https://www.example.org/’) print(r.status_code)
- • asyncio.run(main())
CLI debugging (after installing httpx[cli]):
- • httpx http://httpbin.org/json
Tip: It is recommended to use the context manager (with / async with) for Httpx’s Client/AsyncClient, as this will help manage the connection pool and connection closure, avoiding resource leaks.
Advantages (Why Choose It)
- • API friendly: requests users have almost zero adaptation cost.
- • Both Sync and Async coexist, allowing flexible migration.
- • Supports HTTP/2, leveraging multiplexing to improve efficiency.
- • Good timeout and error handling mechanisms, with clear boundaries, making it less likely to fall into the “default infinite wait” pit.
- • Supports sending requests directly to WSGI/ASGI applications, facilitating unit testing.
- • Complete type annotations, providing a good IDE experience and smoother team collaboration.
- • Active community, comprehensive documentation, and high test coverage (ensuring project quality).
Disadvantages / Points to Note
- • Python version limitation: usually requires Python 3.9+ (depending on the version), older projects may not be able to use it directly.
- • Many optional dependencies: to use features like HTTP/2, socks, brotli, etc., additional extensions need to be installed, so package management needs attention.
- • Ecosystem migration: although the API is similar, it is not 100% fully compatible with requests (edge cases may require changes).
- • As a newer tool, some very niche issues may have a longer bug fix cycle compared to mature projects (however, the community is active, and fixes are usually quick).
- • For ultra-low latency and extreme performance tuning scenarios, more low-level customization may be needed (but most applications will be sufficient).
ConclusionHTTPX is the modern successor to requests: it maintains simplicity and ease of use while addressing modern network needs such as asynchronous support and HTTP/2. If you are working on a new project or want to add async capabilities to an old project, HTTPX is definitely worth a try; just be mindful of the Python version and optional dependencies to ensure a smooth transition and gain better performance with clearer error boundaries.
Project address: https://github.com/encode/httpx