API Navigator Logo

Python Requests PUT Example

Author: Newtum

This example shows how to make an HTTP PUT request using the Python Requests library to update an existing resource on a server.

What is a PUT Request?

An HTTP PUT request is used to update a resource at a specified URI. If the resource does not exist, the API may decide to create it. It is considered an idempotent method, meaning multiple identical PUT requests should have the same effect as a single one.

Making a Basic PUT Request

To make a PUT request, you use the `requests.put()` method and typically provide the data payload you want to send for the update.

import requests
import json

url = 'https://jsonplaceholder.typicode.com/posts/1'

payload = {
    'id': 1,
    'title': 'A new updated title',
    'body': 'This is the updated body content.',
    'userId': 1
}

response = requests.put(url, json=payload)

print('Status Code:', response.status_code)
print('Response JSON:')
print(json.dumps(response.json(), indent=2))