Basic HTTP Requests
Python
import requests
resp = requests.get("https://api.github.com/users/python")
resp.raise_for_status()
data = resp.json()
print(data["public_repos"])
new_post = {"title": "Hello", "body": "World", "userId": 1}
resp = requests.post(
"https://jsonplaceholder.typicode.com/posts",
json=new_post,
timeout=10
)
print(resp.status_code, resp.json())Authentication
Python
# Bearer token
headers = {"Authorization": "Bearer YOUR_TOKEN"}
resp = requests.get("https://api.example.com/data", headers=headers)
# Reuse session (faster, shares cookies)
session = requests.Session()
session.headers.update({"Authorization": f"Bearer {token}"})
resp = session.get("https://api.example.com/profile")Pagination
Python
def fetch_all(url, headers=None):
results, page = [], 1
while True:
resp = requests.get(url, params={"page": page, "per_page": 100}, headers=headers)
resp.raise_for_status()
data = resp.json()
if not data:
break
results.extend(data)
if "next" not in resp.links:
break
page += 1
return resultsRetries & Error Handling
Python
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
session = requests.Session()
retry = Retry(total=3, backoff_factor=1, status_forcelist=[429, 500, 502, 503])
session.mount("https://", HTTPAdapter(max_retries=retry))
try:
resp = session.get("https://api.example.com/data", timeout=30)
resp.raise_for_status()
return resp.json()
except requests.exceptions.Timeout:
print("Timed out")
except requests.exceptions.HTTPError as e:
print(f"HTTP {e.response.status_code}")Tip: Use httpx instead of requests for async code β nearly identical API but supports async/await.