How do I make HTTP requests with the requests library in Python?
· Category: Python Programming
Short answer
The requests library provides a simple API for making HTTP requests. Use requests.get(), requests.post(), and other methods, then inspect the response status code, headers, and content.
Steps
- Install requests:
pip install requests. - Make a request and capture the
Responseobject. - Check
response.status_codeand parseresponse.json()orresponse.text.
import requests
# GET request
response = requests.get("https://httpbin.org/get", params={"key": "value"})
print(response.status_code)
print(response.json())
# POST request
payload = {"username": "alice", "password": "secret"}
response = requests.post("https://httpbin.org/post", json=payload)
print(response.json()["json"])
Tips
- Use
response.raise_for_status()to throw an exception on 4xx/5xx errors. - Set custom headers (like
User-AgentorAuthorization) with theheadersparameter. - Use
timeoutto avoid hanging indefinitely. - Session objects persist cookies and connection pooling across requests.
with requests.Session() as session:
session.headers.update({"Authorization": "Bearer token123"})
r1 = session.get("https://api.example.com/data")
r2 = session.get("https://api.example.com/more")
Common issues
- Forgetting to call
.close()on responses can leak connections; use context managers or Sessions. - JSON parsing fails with
response.json()if the response body is not valid JSON; catchrequests.exceptions.JSONDecodeError. - Hardcoding credentials in source code is a security risk; load them from environment variables.