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

Python requests Module

The requests module lets your program visit web addresses and get information back, like sending a letter and reading the reply. It is how Python talks to websites.
Syntax
python
import requests

response = requests.get("url")
response.status_code
response.json()

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:  # simulates the network call without a real request
    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"})  # dict is sent as form data
    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"}  # metadata sent alongside the request
    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)  # raises instead of blocking forever
except requests.exceptions.RequestException as e:
    print("Request failed:", type(e).__name__)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting timeout= on requests.get(), so the program can hang forever on a slow server.
  2. Using the response body without checking response.status_code or calling raise_for_status().
  3. Sending JSON with data= instead of json=, so the server does not receive a JSON body.

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.