How do you make an HTTP request with `requests` the right way?
BeginnerConceptData formats, HTTP & REST APIs
Answer
import requests
resp = requests.get("https://api.github.com/repos/acme/app/releases/latest",
headers={"Authorization": f"Bearer {token}",
"Accept": "application/vnd.github+json"},
timeout=(3.05, 15))
resp.raise_for_status()
tag = resp.json()["tag_name"]
Three non-negotiables: a timeout (the default is to wait forever), raise_for_status() (requests does not raise on 4xx/5xx), and credentials from the environment or a secrets manager, never hardcoded.
More on Data formats, HTTP & REST APIs
How do you read and write JSON, and what are the common gotchas?BeginnerWhy must you use `yaml.safe_load` rather than `yaml.load`?BeginnerHow do you load a multi-document Kubernetes YAML file?IntermediateHow do you read and write CSV files properly?BeginnerWhy use a `requests.Session`?IntermediateHow do you add retries with exponential backoff to `requests`?Intermediate