← Back to Python Course | Chapter 13: Data Science & Web | Lesson 9 of 14

Python requests Module

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

python
# 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

python
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

python
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

python
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

python
import requests

try:
    requests.get("https://api.example.com", timeout=0.001)
except requests.exceptions.RequestException as e:
    print("Request failed:", type(e).__name__)

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.