HTTPX — The Next-Generation HTTP Client for Python
HTTPX provides the ergonomics of requests with the power of async, HTTP/2, and strict type safety. The same API works in sync and async code, making it the go-to client for modern Python applications.
Safe staging for this asset
This asset is staged first. The copied prompt tells the agent to inspect the staged files and ask before activating scripts, MCP config, or global config.
npx -y tokrepo@latest install 698498fa-37b5-11f1-9bc6-00163e2b0d79 --target codexStages files first; activation requires review of the staged README and plan.
What it is
HTTPX is a Python HTTP client that provides the familiar API of the requests library with added support for async/await, HTTP/2, and strict type annotations. The same httpx.Client API works in both synchronous and asynchronous contexts, making it the go-to HTTP client for modern Python applications that need to evolve from sync to async.
HTTPX targets Python developers building API clients, web services, scrapers, or microservice integrations who want a requests-compatible API with modern protocol support.
How it saves time or tokens
Migrating from requests to an async HTTP client traditionally meant learning a different API (aiohttp). HTTPX uses the same method names and patterns as requests, so the migration is mechanical. Adding HTTP/2 support is a single flag (http2=True). You get connection pooling, automatic redirect handling, and cookie persistence without extra setup.
How to use
- Install HTTPX:
pip install httpx(addhttpx[http2]for HTTP/2 support). - For sync code, use
httpx.get()or create ahttpx.Client()for connection pooling. - For async code, use
async with httpx.AsyncClient() as client:andawait client.get().
Example
import httpx
import asyncio
# Synchronous usage (drop-in requests replacement)
r = httpx.get('https://api.github.com/users/python')
print(r.json()['public_repos'])
# Async usage
async def main():
async with httpx.AsyncClient(http2=True) as client:
r = await client.get('https://httpbin.org/get')
print(r.http_version) # HTTP/2
print(r.status_code)
asyncio.run(main())
# Connection pooling with a persistent client
with httpx.Client(base_url='https://api.example.com') as client:
users = client.get('/users').json()
posts = client.get('/posts').json()
Related on TokRepo
- Web Scraping Tools -- HTTP clients and scraping
- Coding Tools -- Python developer libraries
Common pitfalls
- Creating a new
httpx.Client()per request instead of reusing it. Each client manages its own connection pool and TLS sessions. - Forgetting to install
httpx[http2]for HTTP/2 support. The base package only includes HTTP/1.1. - Using
httpx.get()(module-level) in performance-critical code. Module-level functions create a temporary client each call. Use a persistentClientinstance instead.
Frequently Asked Questions
HTTPX provides the same API as requests but adds async support, HTTP/2, strict type annotations, and a more modern codebase. For synchronous code, HTTPX is a near-drop-in replacement. The main difference is that HTTPX requires explicit client management for connection pooling, while requests uses a global session.
Yes. Install `httpx[http2]` and pass `http2=True` to the client constructor. HTTPX negotiates HTTP/2 via ALPN during the TLS handshake. HTTP/2 multiplexes multiple requests over a single connection, reducing latency for APIs that support it.
Yes. Use `client.stream('GET', url)` to get a streaming response. You can iterate over chunks with `async for chunk in response.aiter_bytes()` or `aiter_lines()`. This is essential for downloading large files or processing server-sent events.
HTTPX has its own middleware system based on transports and event hooks, not requests adapters. You can add custom authentication, logging, or retry logic via event hooks (`event_hooks={'request': [...], 'response': [...]}`). The API differs from requests but is straightforward.
Yes. HTTPX provides `httpx.MockTransport` for testing. You supply a handler function that returns mock responses, and the client uses it instead of making real HTTP calls. This is simpler than mocking at the socket level and integrates cleanly with pytest fixtures.
Citations (3)
- HTTPX GitHub— HTTPX provides sync and async HTTP client with HTTP/2 support
- HTTPX Documentation— requests-compatible API with modern protocol support
- HTTPX Testing Docs— MockTransport for testing without real HTTP calls
Related on TokRepo
Discussion
Related Assets
OkHttp — Modern HTTP Client for Java and Kotlin
A reliable and efficient HTTP client for the JVM and Android with connection pooling, transparent GZIP, response caching, and WebSocket support.
Got — Human-Friendly HTTP Client for Node.js
Got is a lightweight, feature-rich HTTP client for Node.js with built-in retry logic, pagination, caching, and hooks for composable request pipelines.
Prisma — Next-Generation ORM for Node.js & TypeScript
Prisma is a next-generation ORM for Node.js and TypeScript supporting PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB, and CockroachDB. Type-safe database access with auto-generated client and intuitive schema language.
Next.js — The Full-Stack React Framework for the Web
Next.js is the most popular React framework for building full-stack web applications. It provides server-side rendering, static generation, API routes, file-based routing, and React Server Components — making React production-ready out of the box.