Python - Send HTTP PUT Request


Python - Send HTTP PUT Request

HTTP PUT request is used to create or update a resource in a specified server, same as that of HTTP POST, but PUT request being idempotent.

In Python Requests library, requests.put() method is used to send a PUT request to a server over HTTP. You can also send additional data in the PUT request using data parameter.


Examples

1. Send HTTP PUT request to server

In this example, we shall send a HTTP PUT Request to the server at /. We shall also send data in the PUT request.

Python Program

import requests

response = requests.put('https://example.com/api/', data = {'key': 'value'})

Explanation:

  1. The requests.put() function sends an HTTP PUT request to the provided URL with the specified data.
  2. The data parameter is a dictionary containing the key-value pairs you want to send to the server.
  3. The response returned is a Response object, which contains details such as the status code, headers, and content from the server.

Let us print the headers of the response received in the PUT request made in the above example.

Python Program

import requests

response = requests.put('https://example.com/api/', data={'key1': 'value1', 'key2': 'value2'})

print(response.headers)

Output

{'Date': 'Mon, 25 Mar 2019 14:00:23 GMT', 'Content-Type': 'text/html; charset=UTF-8', 'Content-Length': '12140', 'Connection': 'keep-alive', 'Keep-Alive': 'timeout=30', 'Server': 'Apache/2', 'X-Powered-By': 'PHP/A.A.AA', 'Link': '</wp-json/>; rel="https://api.w.org/", </>; rel=shortlink', 'Vary': 'Accept-Encoding', 'Content-Encoding': 'gzip', 'Referrer-Policy': 'no-referrer-when-downgrade'}

2. Send PUT request with JSON Data

Just like in the POST request, PUT requests can also send JSON data. You need to set the Content-Type header to application/json and pass the data in JSON format.

Python Program

import requests
import json

headers = {'Content-Type': 'application/json'}

data = {'name': 'John', 'age': 30}

response = requests.put('https://example.com/api/', headers=headers, data=json.dumps(data))

print(response.json())

Explanation:

  1. The headers dictionary is used to specify the content type as JSON.
  2. The json.dumps() method serializes the Python dictionary into a JSON-formatted string before sending it to the server.
  3. Once the request is processed, the server returns a JSON response, which is parsed and printed using response.json().

Output

{'status': 'success', 'message': 'Data updated successfully'}

3. Handling PUT request with File Data

Another common scenario is to send file data in the PUT request. The file can be uploaded along with other data using the files parameter.

Python Program

import requests

files = {'file': open('example.txt', 'rb')}

response = requests.put('https://example.com/upload', files=files)

print(response.status_code)

Explanation:

  1. To upload a file in a PUT request, we use the files parameter, passing the file opened in binary mode (rb) as the value of the dictionary.
  2. The server processes the file and responds with a status code, which we print out in the program.

Output

200

About HTTP PUT Request

Following are some points to note while working with HTTP PUT requests:

  • PUT requests are never cached.
  • PUT requests do not remain in the browser history.
  • PUT requests cannot be bookmarked.
  • PUT requests have no restrictions on data length.

Difference between POST and PUT

PUT is idempotent, meaning that no matter how many times you send the same PUT request, the result will be the same. On the other hand, POST is not idempotent, and repeated requests may result in different outcomes, such as creating multiple resources.


Summary

In this tutorial, we learned how to send an HTTP PUT request using the requests library. We explored different use cases, such as sending data as form data, JSON data, and uploading files in PUT requests.




Python Libraries