Installing and Setting Up the Requests Library
The requests library is not included in Python's standard library, so you need to install it separately. Run the following command in your terminal:
pip install requests
To verify the installation was successful, run:
import requests
print(requests.__version__)
What Are HTTP Requests and Why Use the Requests Library
HTTP (HyperText Transfer Protocol) is the protocol for data transfer between a client and a server. Requests is a third-party Python library that makes sending HTTP requests much simpler compared to the built-in urllib module.
Key Benefits of Requests
- Simple to use and easy-to-read code
- Supports all HTTP methods (GET, POST, PUT, DELETE, PATCH)
- Automatic JSON data handling
- Built-in support for cookies and sessions
- Flexible header and parameter configuration
- Error and exception handling
- Support for proxy servers and SSL certificates
Core HTTP Methods with Practical Examples
GET Requests: Retrieving Data
A GET request is used to fetch data from a server without modifying it.
import requests
# Simple GET request
response = requests.get('https://api.github.com')
print(response.status_code) # 200
print(response.text) # Response content
Passing Parameters in a GET Request
# Passing parameters using a dictionary
params = {
'q': 'python',
'sort': 'stars',
'order': 'desc',
'page': 1
}
response = requests.get('https://api.github.com/search/repositories', params=params)
print(response.url) # Check the final URL with parameters
POST Requests: Sending Data
A POST request is used to send data to the server and create new resources.
# POST request with form data
data = {
'username': 'test_user',
'password': 'secure_password',
'email': 'test@example.com'
}
response = requests.post('https://httpbin.org/post', data=data)
print(response.json())
POST Request with JSON Data
# Sending JSON data
json_data = {
'title': 'New Post',
'body': 'Post content',
'userId': 1
}
response = requests.post(
'https://jsonplaceholder.typicode.com/posts',
json=json_data
)
print(response.json())
PUT Requests: Updating Data
A PUT request is used to fully update an existing resource.
update_data = {
'id': 1,
'title': 'Updated Title',
'body': 'New content',
'userId': 1
}
response = requests.put(
'https://jsonplaceholder.typicode.com/posts/1',
json=update_data
)
print(f"Status: {response.status_code}")
print(response.json())
PATCH Requests: Partial Updates
PATCH is used for partially updating a resource.
patch_data = {'title': 'Partially Updated Title'}
response = requests.patch(
'https://jsonplaceholder.typicode.com/posts/1',
json=patch_data
)
print(response.json())
DELETE Requests: Removing Data
A DELETE request is used to remove resources from the server.
response = requests.delete('https://jsonplaceholder.typicode.com/posts/1')
print(f"Delete status: {response.status_code}")
Working with HTTP Headers
Headers contain metadata about the request and allow you to pass additional information to the server.
headers = {
'User-Agent': 'Mozilla