Skip to content

Requests - Overview

Requests is an elegant and simple HTTP library for Python, famously described as “HTTP for Humans.” It allows you to send HTTP/1.1 requests with incredible ease, abstracting away the complexities often associated with making web requests in Python. It’s the de facto standard for most Python developers needing to interact with web services and APIs.

The official documentation for Requests, which is an excellent resource, can be found at: https://requests.readthedocs.io/en/latest/

While Python’s standard library includes modules like urllib.request for handling HTTP, Requests offers a significantly more intuitive and user-friendly API. Key advantages include:

  • Simple and clean syntax for all common HTTP methods.
  • Automatic content decoding (e.g., from UTF-8) and decompression (e.g., gzip).
  • Browser-style SSL verification (HTTPS) by default.
  • Session management with cookie persistence and connection pooling.
  • Easy handling of various authentication mechanisms.
  • Effortless file uploads and multipart requests.
  • Support for international domains and URLs.

Installing Requests is straightforward using pip, Python’s package installer. It’s highly recommended to use a virtual environment for your projects to manage dependencies cleanly.

pip install requests

Once installed, you can import it into your Python scripts: import requests.

Requests provides a rich set of features to handle most HTTP communication needs seamlessly:

Supports all standard HTTP methods (GET, POST, PUT, DELETE, HEAD, OPTIONS) via simple functions (e.g., requests.get(), requests.post()). You can easily pass URL parameters, custom headers, and request bodies.

The Response object provides convenient access to: .status_code, .headers, .content (raw bytes), .text (decoded string), and .json() (for parsing JSON responses).

Send custom HTTP headers by passing a dictionary to the headers parameter.

Pass URL parameters (e.g., ?key=value) as a dictionary to the params argument in GET requests.

Send form-encoded data using the data parameter. For JSON payloads, the json parameter automatically serializes Python dictionaries and sets the correct Content-Type header.

Automatically handles cookies received from servers and allows you to send custom cookies.

requests.Session() objects persist parameters (like cookies, authentication, and headers) across multiple requests. They also utilize urllib3’s connection pooling, enhancing performance for repeated requests to the same host.

Verifies SSL certificates for HTTPS requests by default, crucial for secure communication. Offers options for custom CA bundles.

Includes built-in support for common authentication schemes like HTTP Basic Auth and Digest Auth, and allows for custom authentication handlers.

Essential for robust applications, timeouts can be specified for requests to prevent indefinite blocking if a server is unresponsive.

Provides clear exceptions for network problems or timeouts. The response.raise_for_status() method can be used to raise an HTTPError for bad HTTP responses (4xx or 5xx). Redirection handling is automatic but configurable.

Here’s a glimpse of Requests in action, fetching data from a public API:

import requests
URL = 'https://api.github.com/events'
try:
# Make a GET request
response = requests.get(URL, timeout=5) # Added a timeout
# Check if the request was successful (status code 200-299)
response.raise_for_status() # Raises an HTTPError for bad responses
print(f"Status Code: {response.status_code}")
print(f"Content-Type: {response.headers.get('Content-Type')}")
# Get the JSON response (list of events)
events = response.json()
print(f"Number of events received: {len(events)}")
if events:
print(f"Type of the first event: {events[0]['type']}")
except requests.exceptions.HTTPError as http_err:
print(f'HTTP error occurred: {http_err} - Status: {http_err.response.status_code}')
except requests.exceptions.ConnectionError as conn_err:
print(f'Connection error occurred: {conn_err}')
except requests.exceptions.Timeout as timeout_err:
print(f'Timeout error occurred: {timeout_err}')
except requests.exceptions.RequestException as err:
print(f'An unexpected error occurred: {err}')
Status Code: 200
Content-Type: application/json; charset=utf-8
Number of events received: 30
Type of the first event: PushEvent
(Note: Actual event type and count may vary)

This tutorial will delve deeper into these features, equipping you to effectively use the Requests library for various web interaction tasks in your Python projects.