The requests.put
function in Python’s requests
module is used to make HTTP PUT requests. This function is typically used to update a resource on a server.
Table of Contents
- Introduction
requests.put
Function Syntax- Examples
- Basic Usage
- Sending Form Data
- Sending JSON Data
- Sending Headers
- Handling JSON Responses
- Real-World Use Case
- Conclusion
Introduction
The requests.put
function is part of the requests
module, which makes it easy to make HTTP requests in Python. You can use this function to send data to a web server to update an existing resource and get responses.
requests.put Function Syntax
Here’s how you use the requests.put
function:
import requests
response = requests.put(url, **kwargs)
Parameters:
url
: The URL for the request.**kwargs
: Optional arguments to customize the request. Common ones include:data
: Dictionary to send in the body of the request as form data.json
: Dictionary to send in the body of the request as JSON.headers
: Dictionary of HTTP headers to send with the request.
Returns:
- A
Response
object containing the server’s response to the HTTP request.
Examples
Basic Usage
Send a simple PUT request to a URL.
import requests
response = requests.put('https://jsonplaceholder.typicode.com/posts/1')
print(response.json())
Output:
{'id': 1}
Sending Form Data
Send form data in the body of a PUT request.
import requests
data = {'title': 'foo', 'body': 'bar', 'userId': 1}
response = requests.put('https://jsonplaceholder.typicode.com/posts/1', data=data)
print(response.json())
Output:
{'title': 'foo', 'body': 'bar', 'userId': '1', 'id': 1}
Sending JSON Data
Send JSON data in the body of a PUT request.
import requests
json_data = {'title': 'foo', 'body': 'bar', 'userId': 1}
response = requests.put('https://jsonplaceholder.typicode.com/posts/1', json=json_data)
print(response.json())
Output:
{'title': 'foo', 'body': 'bar', 'userId': 1, 'id': 1}
Sending Headers
Send custom headers with a PUT request.
import requests
headers = {'Authorization': 'Bearer your_token'}
response = requests.put('https://jsonplaceholder.typicode.com/posts/1', headers=headers)
print(response.json())
Output:
{'id': 1}
Handling JSON Responses
Send a PUT request and parse the JSON response.
import requests
json_data = {'title': 'foo', 'body': 'bar', 'userId': 1}
response = requests.put('https://jsonplaceholder.typicode.com/posts/1', json=json_data)
data = response.json()
print(data)
Output:
{'title': 'foo', 'body': 'bar', 'userId': 1, 'id': 1}
Real-World Use Case
Updating Data on an API
Update data on an API that requires authentication.
import requests
url = 'https://jsonplaceholder.typicode.com/posts/1'
headers = {'Authorization': 'Bearer your_token'}
json_data = {'title': 'foo', 'body': 'bar', 'userId': 1}
response = requests.put(url, headers=headers, json=json_data)
data = response.json()
print(data)
Output:
{'title': 'foo', 'body': 'bar', 'userId': 1, 'id': 1}
Conclusion
The requests.put
function is a simple and effective way to make HTTP PUT requests in Python. You can use it to update data on web servers, include custom headers, and handle JSON responses. This function makes it easy to interact with web services and APIs.