๐Ÿ“ LLM & AI

httpx: A Modern HTTP Client for Python

P
Author
PyLand Team
๐Ÿ“…
Published
30.06.2026
โฑ๏ธ
Reading time
1 min
๐Ÿ‘๏ธ
Views
329
๐ŸŒฟ
Level
Medium

httpx is a next-generation HTTP client. Its interface is similar to requests, but it supports async/await out of the box.

Installation

uv add httpx

Basic Requests

import httpx

# GET request
response = httpx.get("https://wttr.in/Moscow?format=j1", timeout=10)
response.raise_for_status()  # raises an exception on 4xx/5xx
data = response.json()

# GET with query parameters
response = httpx.get(
    "https://api.example.com/search",
    params={"q": "python", "limit": 10},
    timeout=10
)

# POST with JSON
response = httpx.post(
    "https://api.example.com/data",
    json={"key": "value"},
    headers={"Authorization": "Bearer token"},
    timeout=10
)

timeout โ€” Always Set It

# Without a timeout, a request can hang forever
response = httpx.get(url, timeout=10)      # 10 seconds
response = httpx.get(url, timeout=None)    # no limit (bad practice)

# Separate timeouts for connect and read
timeout = httpx.Timeout(connect=5.0, read=30.0)
response = httpx.get(url, timeout=timeout)

raise_for_status()

def fetch_json(url: str) -> dict:
    try:
        response = httpx.get(url, timeout=10)
        response.raise_for_status()
        return response.json()
    except httpx.TimeoutException as exc:
        raise RuntimeError("The server did not respond within 10 seconds") from exc
    except httpx.HTTPStatusError as exc:
        raise RuntimeError(
            f"HTTP {exc.response.status_code}: {exc.response.text}"
        ) from exc

Reusing the Client

# Better to create a client once for multiple requests
with httpx.Client(timeout=10, base_url="https://api.example.com") as client:
    r1 = client.get("/users")
    r2 = client.get("/posts")

Async Client

import asyncio
import httpx

async def fetch(url: str) -> dict:
    async with httpx.AsyncClient(timeout=10) as client:
        response = await client.get(url)
        response.raise_for_status()
        return response.json()

# Parallel requests
async def fetch_all(urls: list[str]) -> list[dict]:
    async with httpx.AsyncClient(timeout=10) as client:
        tasks = [client.get(url) for url in urls]
        responses = await asyncio.gather(*tasks)
        return [r.json() for r in responses]

httpx vs requests

requests httpx
Sync โœ… โœ…
Async โŒ โœ…
HTTP/2 โŒ โœ…
Type hints partial full
API identical identical

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

asyncio in Python: Asynchronous Programming

asyncio is Python's standard library for asynchronous code. It lets you execute multiple tasks "simultaneously"...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 316
๐Ÿ“

SQLite in Python: Persistent Memory for Agents

SQLite is a relational database built into Python. It stores data in a single file...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 348
๐Ÿ“

AI Agents: ReAct Loop and Autonomous Actions

A chatbot answers questions. An agent takes action: it calls tools, retrieves real data, and...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 334
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Neural Networks in Code: 5 AI Projects in Python with Claude Open course curriculum