If you’re new to async Python, you’ve probably seen two popular libraries: httpx and aiohttp. Both help you make HTTP requests, but they work a little differently. Here’s an easy breakdown to help you understand them.
✅ httpx: What’s Good
- Very easy to learn if you’ve used requests before
- Works in both sync and async mode
- Supports HTTP/2
- Great for small or medium projects
⚠️ httpx: Things to Consider
- Not as powerful for huge streaming tasks
- Fewer advanced features compared to aiohttp
✅ aiohttp: What’s Good
- Very stable and widely used
- Handles a lot of concurrent requests really well
- Strong support for streaming data
- Can also be used to build web servers
⚠️ aiohttp: Things to Consider
- Async only, no sync mode
- Has a bit more setup and code to write
Example: Fetching JSON from jsonsilo.com
Here’s a simple example showing what the syntax looks like with each library:
Using httpx:
import httpx
import asyncio
async def main():
async with httpx.AsyncClient() as client:
r = await client.get("https://api.jsonsilo.com/f68c0415-10c0-42bd-ad82-969ff7c7c6fc")
print(r.json())
asyncio.run(main())
Using aiohttp:
import aiohttp
import asyncio
async def main():
async with aiohttp.ClientSession() as session:
async with session.get("https://api.jsonsilo.com/f68c0415-10c0-42bd-ad82-969ff7c7c6fc") as resp:
print(await resp.json())
asyncio.run(main())
Which one should you use??
- Choose httpx if you want something simple and modern.
- Choose aiohttp if you expect a lot of traffic or need more performance.
Both are great options and you can’t really go wrong starting with either.
Top comments (0)