Python requests Module
In this page:
Introduction to requests
requests is the standard third-party library for making HTTP calls from Python, favored over the built-in urllib for its much simpler API. Install it with pip install requests before running any of these examples, since it isn't part of Python's standard library.
Example: Introduction to requests
# pip install requests
import requests
print(requests.__name__)
Sending GET Requests
A GET request retrieves data from a server without modifying anything server-side. Calling requests.get(url) returns a Response object whose .text gives the raw response body and .status_code gives the numeric HTTP status, letting you check success before trusting the returned data.
Example: Sending GET Requests
from unittest.mock import patch
import requests
with patch("requests.get") as mock_get:
mock_get.return_value.status_code = 200
mock_get.return_value.text = "response body"
response = requests.get("https://api.example.com/data")
print(response.status_code, response.text)
Sending POST Requests
A POST request sends data to a server, typically to create or update a resource. Passing a dictionary to the data parameter of requests.post() automatically encodes it as form data in the request body, matching how an HTML form submission would send the same information.
Example: Sending POST Requests
from unittest.mock import patch
import requests
with patch("requests.post") as mock_post:
mock_post.return_value.status_code = 201
response = requests.post("https://api.example.com/users", data={"name": "Alex"})
print(response.status_code)
Handling Request Headers
HTTP headers carry metadata alongside a request -- like Content-Type to tell the server what format the body is in, or a custom User-Agent to identify your client. Pass a dictionary to the headers parameter of any request method to set them.
Example: Handling Request Headers
from unittest.mock import patch
import requests
with patch("requests.get") as mock_get:
mock_get.return_value.status_code = 200
headers = {"User-Agent": "MyApp/1.0"}
requests.get("https://api.example.com", headers=headers)
mock_get.assert_called_with("https://api.example.com", headers=headers)
print("Headers sent:", headers)
Handling Timeouts and Exceptions
Network calls can hang indefinitely if a server is slow or unresponsive, so passing timeout=N (seconds) to a request tells requests to raise an exception rather than block forever. Wrapping calls in a try/except requests.exceptions.RequestException block lets your program handle both timeouts and connection failures gracefully.
Example: Handling Timeouts and Exceptions
import requests
try:
requests.get("https://api.example.com", timeout=0.001)
except requests.exceptions.RequestException as e:
print("Request failed:", type(e).__name__)
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Python NumPy Introduction
- Python NumPy Arrays
- Python Pandas Introduction
- Python Pandas DataFrame
- Python Matplotlib Basics
- Python Data Visualization
- Python Statistics Module
- Python CSV & Data Analysis
- Python requests Module
- Python JSON & APIs
- Python Web Scraping Basics
- Python Flask Introduction
- Python Django Introduction
- Python MongoDB