Introduction
The requests
library in Python is a powerful and user-friendly tool for making HTTP requests. It abstracts the complexities of making requests and handling responses, making it easier to interact with web APIs and other web resources.
This guide cover the basics of the Python requests
library.
Prerequisites
Before you begin:
- Deploy a VPS server. For instance, Ubuntu 24.04.
- Create a non-root sudo user.
- Install Python.
- Install the
requests
library.
Send GET
Requests
The simplest way to make a request with the requests
library is by using the get
method. Here's a basic example:
-
Install the
requests
library, run:Bash$ pip install requests
-
Enter the following Python code in a new file, for instance,
sample.py
.Pythonimport requests response = requests.get('https://jsonplaceholder.typicode.com/posts') print(response.status_code) print(response.json())
-
Run the sample.
CONSOLE$ python3 sample.py
This sends a GET request to the specified URL and prints the status code and the JSON response.
Send POST
Requests
To send a POST request, use the POST
method. You can include data in the request body:
import requests
data = {
'title': 'foo',
'body': 'bar',
'userId': 1
}
response = requests.post('https://jsonplaceholder.typicode.com/posts', json=data)
print(response.status_code)
print(response.json())
This sends a POST
request with JSON data and prints the status code and the JSON response.
Send PUT
Requests
The PUT
method is used to update resources. Here's an example:
import requests
data = {
'id': 1,
'title': 'foo',
'body': 'bar',
'userId': 1
}
response = requests.put('https://jsonplaceholder.typicode.com/posts/1', json=data)
print(response.status_code)
print(response.json())
This sends a PUT request to update the resource with the specified ID and prints the status code and the JSON response.
Send DELETE
Requests
To delete a resource, use the DELETE
method:
import requests
response = requests.delete('https://jsonplaceholder.typicode.com/posts/1')
print(response.status_code)
print(response.json())
This sends a DELETE request to remove the resource with the specified ID and prints the status code and the JSON response.
SenD PATCH
Requests
The PATCH
method is used to apply partial modifications to a resource. Here's an example:
import requests
data = {
'title': 'foo updated'
}
response = requests.patch('https://jsonplaceholder.typicode.com/posts/1', json=data)
print(response.status_code)
print(response.json())
This sends a PATCH request to update the title of the resource with the specified ID and prints the status code and the JSON response.
Handle Errors
It's important to handle errors that may occur during the request. You can use the raise_for_status
method to raise an exception for HTTP errors:
import requests
try:
response = requests.get('https://jsonplaceholder.typicode.com/posts')
response.raise_for_status()
except requests.exceptions.HTTPError as err:
print(f'HTTP error occurred: {err}')
except Exception as err:
print(f'Other error occurred: {err}')
else:
print('Success!')
This example handles HTTP errors and prints a success message if the request is successful.
Conclusion
This guide explains how to use the requests
library in Python to make HTTP requests, including sending GET, POST, PUT, DELETE, and PATCH requests. The requests
library simplifies the process of interacting with web APIs and other web resources, making it a valuable tool for developers. By understanding how to use this library effectively, you can create more dynamic and responsive applications.