← Back to Chapters

Python Requests Module

? Python Requests Module

⚡ Quick Overview

The requests module in Python is a powerful library used to make HTTP requests. It simplifies working with web APIs, downloading content, and sending data over the internet using methods like GET and POST.

? Key Concepts

  • HTTP Methods: Commonly used methods are GET (fetch data) and POST (send data).
  • Response Object: Contains data sent back by the server (status code, headers, body, etc.).
  • Status Codes: Indicate the result of the request (e.g., 200 OK, 404 Not Found).
  • Query Parameters: Extra data sent in the URL using params.
  • Headers: Extra metadata (like User-Agent, Authorization) sent with the request.
  • Error Handling: Use exceptions and raise_for_status() to handle failures safely.

? Syntax & Theory

A typical HTTP request using requests follows this pattern:

? Basic Request Pattern
import requests

url = "https://example.com"
params = {"q": "python"}
headers = {"User-Agent": "MyApp/1.0"}

response = requests.get(url, params=params, headers=headers)

print(response.status_code)   # HTTP status code
print(response.text)          # Response body as text

? Code Examples

? Installing Requests

Install the requests module using pip:

? Install Using pip
pip install requests # Install the requests package

? Sending a GET Request

GET requests are used to fetch data from a URL.

? Simple GET Request
import requests

# Send a GET request to the GitHub API root
response = requests.get("https://api.github.com")

# Print the HTTP status code
print(response.status_code)

# Print the response body as text
print(response.text)

? Sending a POST Request

POST requests are used to send data to a server.

✉️ POST With Form Data
import requests

# Data that will be sent in the POST request body
payload = {"username": "test", "password": "12345"}

# Send a POST request with form data
response = requests.post("https://httpbin.org/post", data=payload)

# Print the HTTP status code
print(response.status_code)

# Parse and print the JSON response
print(response.json())

? Handling the Response Object

The response object provides status, headers, and the response body.

? Inspect Response Details
import requests

response = requests.get("https://api.github.com")
print("Status Code:", response.status_code)
print("Headers:", response.headers)
print("Content:", response.text[:100])  # First 100 characters

? Passing Parameters in URL

Use the params argument to send query parameters in a GET request.

? GET With Query Parameters
import requests

# Query parameters to send in the URL
params = {"q": "python", "page": 1}

# Send GET request with query parameters
response = requests.get("https://httpbin.org/get", params=params)

# Final URL with encoded query string
print(response.url)

# JSON body of the response
print(response.json())

? Using Custom Headers

Some APIs require custom headers like User-Agent or Authorization.

? GET With Custom Headers
import requests

# Custom headers sent with the request
headers = {"User-Agent": "MyApp/1.0"}

# Send GET request including custom headers
response = requests.get("https://httpbin.org/headers", headers=headers)

# JSON response showing the headers the server received
print(response.json())

?️ Handling Errors Safely

Always check for errors to avoid crashes due to failed requests.

? Error Handling With Exceptions
import requests

try:
    response = requests.get("https://api.github.com/unknown")
    response.raise_for_status()  # Raise exception for HTTP errors
except requests.exceptions.HTTPError as err:
    print("HTTP Error:", err)
except requests.exceptions.RequestException as e:
    print("Error:", e)

? Live Output & Explanation

? What You Can Expect

  • response.status_code might print 200 for success, 404 for not found, etc.
  • response.text contains the raw HTML or JSON string returned by the server.
  • response.json() parses JSON into a Python dictionary, which is easier to work with.
  • If raise_for_status() is called on a non-2xx status, an exception will be raised.

Use these properties to validate that a request succeeded before processing the data further.

? Common Use Cases

  • Consuming public APIs (GitHub, OpenWeather, etc.).
  • Submitting form data to a web server.
  • Downloading files like images, PDFs, or datasets.
  • Automating tasks such as periodic data collection or monitoring.

? Tips & Best Practices

  • Use response.json() to parse JSON responses from APIs.
  • Always check response.status_code or use raise_for_status() before processing data.
  • Use params for GET queries and data or json for POST requests.
  • Set custom headers when required by APIs (e.g., authentication tokens).
  • For multiple requests to the same host, use requests.Session() to reuse connections efficiently.

? Try It Yourself

  • Make a GET request to https://httpbin.org/get and print the JSON response.
  • Send a POST request with sample form data to https://httpbin.org/post and inspect the result.
  • Add custom headers (e.g., User-Agent) and verify them in the response.
  • Call an invalid URL and handle the exception using try/except.
  • Experiment with params in GET requests and observe how the URL changes.