The patch() method of requests module is used to partially modify any resource on the server. This method only sends the content that requires modification rather than sending the entire content as in the case of put() method.

Python Requests patch():

The patch() requests may or may not be idempotent, which means multiple requests may or may not have the same result as they differ in the parameter.

In other words, the patch() method is same as the put() method. But, we can use this method to replace an existing resource partially. Thus, Patch() is faster than put() method. The common code returned by the response object is as shown below :

  • 405 – the method is not allowed
  • 202 – the action is pending
  • 200 – the action has been taken and the response message describes the status
  • 204 – the action has been taken, but no further information is available

Method Signature

The signature for the patch method is as shown below.

requests.patch(url, params={key:value}, args)

Method parameters and return type

  • Here, the patch method takes three parameters as listed below.
    • URL – Mandatory parameter. It specifies the URL
    • params – It sends a dictionary, list of tuples, bytes to send us a query string. This is an optional parameter.
    • args – It can pass several arguments like cookies, headers, proxies, timeout, stream, auth and cert. These arguments are optional.
  • However, it returns a response object.

Python patch method Examples:

Example 1: In this case, we will send requests to a URL and print the status code and reason for its response.

#Importing requests module
import requests

# variable storing url 
url="https://google.com"

# Making a patch request
r = requests.patch(url)
  
# Checking status code and reason of response 
print("Status code for Google- ",r.status_code)
print("Reason for Google - ",r.reason)

Output

Status code for Google - 405
Reason for Google - Method Not Allowed

Example 2: In this case, we will send headers to the request body to create or update a resource on the given URL.

#Importing requests module
import requests
# variable storing url and headers  
url = "https://httpbin.org/patch"
h = {"k1":"Python Programming"}

#Using the headers parameter to set the HTTP headers:
x = requests.patch(url, headers=h)

print("Status code - ",x.status_code)
print("Reason - ",x.reason)

Output

Status code - 200
Reason - OK

Example 3: In this case, this method returns the response’s status if it arrives before the timeout. However, if not, it will raise the ReadTimeoutException.

#Importing requests module
import requests

# variable storing url 
url = "http://nhk.e6a.mytemp.website/python"

#Using the timeout parameter 
x = requests.patch(url, timeout=10)

print("Status code - ",x.status_code)
print("Reason - ",x.reason)

Output

Status code - 405
Reason - Method Not Allowed

Conclusion

Hence, the patch() method performs the partial update operations for the resource on the server.

References

Happy Learning 🙂