Use this when making HTTP calls with requests.
requests
1. requests for HTTP APIs.
2. Always set timeouts.
3. raise_for_status.
4. Sessions with retries/backoff.
5. Verify TLS (never verify=False in production).
request → API → response → status check
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
requestsPrimary Commands
Configuration and operational commands for this Topic.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Common mistakes Narrow the fault
- No
timeout=(waits forever). verify=Falseto get past certificate errors.- Ignoring pagination (
next).
- Always set a timeout.
- Call raise_for_status() to catch HTTP errors.
- Use a Session to reuse connections.
python3 -c "import requests;r=requests.get('https://example.com',timeout=10);print(r.status_code)"— request with timeout- raise_for_status() — error handling
- Session with retries (HTTPAdapter) — resilience
ReferenceImportant terms & boundaries2 sections · depth on demand
How it works
A Session reuses TCP/TLS connections and default headers across calls, which is faster and keeps auth in one place. raise_for_status() turns 4xx/5xx into exceptions, r.json() parses the body, and verify= points to a CA bundle when the API uses an internal CA. Paginated APIs return a next URL to follow until it is null.
Example
import requests
s = requests.Session()
s.headers["Authorization"] = f"Token {token}"
r = s.get(f"{nb}/api/dcim/devices/", params={"site": "oslo"}, timeout=10)
r.raise_for_status()
devices = r.json()["results"]
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Python Automation > requests. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://docs.python.org/3/ · confidence 94%