Last modified: Jan 30, 2025 By Alexander Williams
Python httpx.AsyncClient() Guide: Async HTTP Requests
Python's httpx library is a powerful tool for making HTTP requests. The AsyncClient()
class allows you to perform asynchronous HTTP requests efficiently. This guide will walk you through its usage.
Table Of Contents
What is httpx.AsyncClient()?
The AsyncClient()
class in httpx is designed for asynchronous HTTP requests. It is ideal for handling multiple requests concurrently, making it faster than synchronous methods.
Setting Up httpx
To use AsyncClient()
, you need to install the httpx library. You can do this using pip:
pip install httpx
Basic Usage of httpx.AsyncClient()
Here’s a simple example of how to use AsyncClient()
to make an asynchronous GET request:
import httpx
import asyncio
async def fetch_data():
async with httpx.AsyncClient() as client:
response = await client.get('https://jsonplaceholder.typicode.com/posts/1')
return response.json()
data = asyncio.run(fetch_data())
print(data)
This code fetches data from a sample API and prints the JSON response. The AsyncClient()
ensures the request is non-blocking.
Handling Multiple Requests
One of the main advantages of AsyncClient()
is its ability to handle multiple requests concurrently. Here’s an example:
import httpx
import asyncio
async def fetch_multiple_urls():
urls = [
'https://jsonplaceholder.typicode.com/posts/1',
'https://jsonplaceholder.typicode.com/posts/2',
'https://jsonplaceholder.typicode.com/posts/3'
]
async with httpx.AsyncClient() as client:
tasks = [client.get(url) for url in urls]
responses = await asyncio.gather(*tasks)
return [response.json() for response in responses]
data = asyncio.run(fetch_multiple_urls())
print(data)
This script fetches data from multiple URLs simultaneously, showcasing the power of asynchronous programming.
Error Handling
When making HTTP requests, errors can occur. It’s important to handle them gracefully. Here’s how you can do it with AsyncClient()
:
import httpx
import asyncio
async def fetch_with_error_handling():
async with httpx.AsyncClient() as client:
try:
response = await client.get('https://jsonplaceholder.typicode.com/invalid')
response.raise_for_status()
except httpx.HTTPStatusError as e:
print(f"HTTP error occurred: {e}")
except httpx.RequestError as e:
print(f"Request error occurred: {e}")
asyncio.run(fetch_with_error_handling())
This example demonstrates how to catch and handle HTTP and request errors.
Advanced Features
AsyncClient()
supports advanced features like timeouts, custom headers, and more. Here’s an example with custom headers:
import httpx
import asyncio
async def fetch_with_headers():
headers = {'Authorization': 'Bearer YOUR_TOKEN'}
async with httpx.AsyncClient(headers=headers) as client:
response = await client.get('https://api.example.com/data')
return response.json()
data = asyncio.run(fetch_with_headers())
print(data)
This script sends a request with custom headers, which is useful for authenticated APIs.
Conclusion
The AsyncClient()
in httpx is a powerful tool for making asynchronous HTTP requests in Python. It is efficient, easy to use, and supports advanced features. Whether you're handling multiple requests or need custom configurations, AsyncClient()
has you covered.
For more information on related topics, check out our guides on httpx.Client() and httpx.stream().