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

  1. Install requests: pip install requests.
  2. Make a request and capture the Response object.
  3. Check response.status_code and parse response.json() or response.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-Agent or Authorization) with the headers parameter.
  • Use timeout to 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; catch requests.exceptions.JSONDecodeError.
  • Hardcoding credentials in source code is a security risk; load them from environment variables.