API Navigator Logo

Python Send PATCH Request

Author: Newtum

This guide shows how to send an HTTP PATCH request using the Python Requests library. PATCH is used for making partial updates to an existing resource.

PATCH vs. PUT

A `PUT` request typically replaces the entire resource with the new data provided. In contrast, a `PATCH` request only applies a partial modification to the resource, updating only the fields included in the payload.

Making a PATCH Request

You can make a PATCH request using `requests.patch()`. The payload should contain only the fields you want to change.

import requests
import json

# The resource to update
url = 'https://jsonplaceholder.typicode.com/posts/1'

# We only want to update the title
payload = {
    'title': 'A partially updated title'
}

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

print('Status Code:', response.status_code)
print('Response JSON:')
print(json.dumps(response.json(), indent=2))
# The response body shows the resource with the updated title