Python Requests Library: HTTP Requests Guide | PythonLib

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to the Requests Library

Requests is a simple yet powerful library for handling HTTP requests in Python. It provides a clean interface for working with REST APIs, downloading web data, uploading files, and handling authentication. Thanks to its concise syntax and extensive feature set, Requests has become the de facto standard HTTP client for Python.

Created by Kenneth Reitz in 2011, the Requests library was designed to make HTTP requests in Python as straightforward and user-friendly as possible. It's built on top of urllib3 and offers a high-level API for working with the HTTP protocol.

Why Choose Requests Over urllib?

  • Cleaner syntax — intuitive methods and parameters
  • Automatic cookie and redirect handling — no extra configuration needed
  • Easy JSON, headers, and form data handling — built-in support for popular formats
  • Sessions, proxies, and authentication support — a complete toolkit for web services
  • All HTTP methods supported — GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS
  • Automatic content decoding — intelligent encoding detection
  • Built-in SSL support — secure connections by default

Installation and Setup

Install using pip:

pip install requests

To upgrade to the latest version:

pip install --upgrade requests

Import the library:

import requests

Core HTTP Request Methods

Sending GET Requests

GET requests are used to retrieve data from a server:

response = requests.get('https://api.example.com/data')
print(response.status_code)  # Response status code
print(response.text)         # Response body as text

Fetching JSON data:

response = requests.get('https://api.example.com/users')
data = response.json()  # Automatically converts to a Python dictionary
print(data)

Sending POST Requests

POST requests are used to send data to a server:

payload = {'username': 'admin', 'password': '123'}
response = requests.post('https://api.example.com/login', data=payload)

Sending JSON data:

user_data = {'name': 'John', 'email': 'john@example.com'}
response = requests.post('https://api.example.com/users', json=user_data)

Using PUT, PATCH, and DELETE

# PUT - fully update a resource
requests.put('https://api.example.com/user/1', json={'name': 'New Name', 'email': 'new@example.com'})

# PATCH - partially update a resource
requests.patch('https://api.example.com/user/1', json={'active': True})

# DELETE - remove a resource
requests.delete('https://api.example.com/user/1')

# HEAD - retrieve only headers
response = requests.head('https://api.example.com/user/1')
print(response.headers)

Working with Query Parameters

Pass URL parameters using a dictionary:

params = {'search': 'python', 'limit': 10, 'page': 1}
response = requests.get('https://api.example.com/search', params=params)
print(response.url)  # Shows the full URL with parameters

You can also pass parameters as a list of tuples:

params = [('tag', 'python'), ('tag', 'web'), ('limit', 20)]
response = requests.get('https://api.example.com/posts', params=params)                    

Recommendations