httpx

0.28.1 · active · verified Fri Mar 27

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

Install

Imports

Quickstart

Minimal sync and async HTTP GET requests using httpx 0.28.x. Uses Client/AsyncClient as context managers for proper connection management.

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())

view raw JSON →