Working with APIs using requests
Scraping is a fallback when vendors expose no contract; REST/JSON endpoints behave better for automation—they version responses, authenticate explicitly, and document rate limits.
pip install requests
📚 Prerequisites
- JSON literacy (dict/list structures in Python).
🎯 What you'll master
- GET/POST with JSON bodies, headers (
Authorization), and query strings. - Handle transient failures robustly (
raise_for_status, retry policies conceptually).
GET with params
import requests
resp = requests.get(
"https://httpbin.org/get",
params={"page": "2"},
headers={"Accept": "application/json"},
timeout=10,
)
resp.raise_for_status()
print(resp.json()["args"])
POST JSON
payload = {"title": "Sync job", "status": "queued"}
resp = requests.post(
"https://httpbin.org/post",
json=payload,
timeout=10,
)
resp.raise_for_status()
json= auto-serializes and sets appropriate Content-Type.
Session reuse
Use requests.Session() when hitting the same origin repeatedly—it keeps cookies and connection pooling warm.
💡 Key takeaways
- Log correlation IDs embedded in upstream responses—they accelerate partner support traces.
➡️ Next steps
Send automated notifications responsibly in Email automation with smtplib and email.