HTTP Client Libraries: requests & httpx
Chapter goals: understand the Python HTTP client ecosystem, grasp how requests and httpx differ in positioning, and set up your development environment.
1.1 Why You Need an HTTP Client Library
Python's standard library provides urllib, but its API is primitive and verbose. In production, developers almost always reach for a third-party library:
# Standard library urllib — you must handle encoding and closing connections manually
import urllib.request
req = urllib.request.Request('https://api.github.com/user', headers={'User-Agent': 'python'})
with urllib.request.urlopen(req) as resp:
data = resp.read().decode('utf-8')
# requests — one line does it
import requests
resp = requests.get('https://api.github.com/user')
print(resp.json())After its release in 2011, requests quickly became the de facto standard for HTTP in Python. After 2020, httpx arrived as a "modern requests", offering native async support and HTTP/2.
1.2 requests vs httpx Comparison
| Feature | requests | httpx |
|---|---|---|
| API style | Synchronous, concise | Sync + async dual API |
| HTTP/2 | ❌ | ✅ |
| WebSocket | ❌ | ✅ |
| Type annotations | ❌ | ✅ (Pydantic) |
| Streaming responses | Supported | Supported |
| Ecosystem maturity | Very mature | Growing fast |
| Best suited for | Scripts, CLIs, legacy projects | Modern async apps, API clients |
Suggested learning path:
- Learn requests first (simple API, abundant resources)
- Then learn httpx (adds async capability)
- Their APIs are highly compatible, so switching costs are low
1.3 Installation and Environment Setup
# Install requests
pip install requests
# Install httpx (HTTP/2 support needs an extra dependency)
pip install httpx
pip install "httpx[http2]" # if you need HTTP/2 support
# Verify installation
python -c "import requests; print(requests.__version__)"
python -c "import httpx; print(httpx.__version__)"It is recommended to manage dependencies with uv or poetry to keep environments isolated.
1.4 Your First Request: GET an API
import requests
# The simplest GET request
resp = requests.get('https://httpbin.org/get')
# Check the status code
print(resp.status_code) # 200
# Automatic JSON parsing
data = resp.json()
print(data['headers'])
# Text response
print(resp.text[:100])1.5 Chapter Summary
- Python has two main HTTP client choices: requests (the veteran sync library) and httpx (modern, full-featured);
- requests keeps the API simple; httpx adds async and HTTP/2;
- Their APIs are similar — once you master one, learning the other is quick.
🧪 随堂测验
点击你认为正确的选项。答错时会展示正确答案与原因解析。
1. Which of the following libraries provides native async HTTP support?
2. Who is the author of the requests library?
3. Which of these is NOT a core feature httpx adds over requests?
4. What is the command to install httpx with HTTP/2 support enabled?
🛠️ Hands-On Exercises
- Install requests and httpx, and print both libraries' version numbers.
- Use requests to call
https://httpbin.org/getand print the response status code and headers. - Perform the same request with httpx and compare the differences between the two APIs.