
httpx Is What requests Should Have Been — Async, HTTP/2, and Better Defaults
I stopped using the requests library 2 years ago. httpx does everything requests does, plus async, HTTP/2, and better defaults. Here is why and how to switch. Install pip install httpx Drop-in replacement. Most requests code works with zero changes. The Basics import httpx # GET request (identical to requests) resp = httpx . get ( " https://httpbin.org/get " ) print ( resp . status_code ) # 200 print ( resp . json ()) # POST with JSON resp = httpx . post ( " https://httpbin.org/post " , json = { " key " : " value " }) # Headers resp = httpx . get ( " https://api.github.com/user " , headers = { " Authorization " : " Bearer token " }) Why httpx > requests 1. Async Support (Built-in) import httpx import asyncio async def fetch_all ( urls ): async with httpx . AsyncClient () as client : tasks = [ client . get ( url ) for url in urls ] responses = await asyncio . gather ( * tasks ) return [ r . json () for r in responses ] # Fetch 10 URLs in parallel instead of sequentially urls = [ f " htt
Continue reading on Dev.to Tutorial
Opens in a new tab




