make a http put request in python

To make an HTTP PUT request in Python, you can use the requests library. Here's an example code snippet:

main.py
import requests

# set the url and payload for the request
url = 'http://example.com/api/user/123'
payload = {'name': 'John Doe', 'age': 30}

# send the request
response = requests.put(url, json=payload)

# print the response status code and content
print(response.status_code)
print(response.content)
302 chars
13 lines

In this example, we first import the requests library. Then, we set the URL for the PUT request and the payload, which contains the data we want to include in the request. We use the json argument to set the payload as JSON data.

Next, we send the PUT request using the requests.put method and pass in the URL and payload as arguments. This sends the HTTP PUT request to the specified URL with the given payload.

Finally, we print the status code and content of the response using the response.status_code and response.content properties, respectively.

related categories

gistlibby LogSnag