Python

Python Requests Delete Method

Like the programming languages, Python also has multiple libraries to perform different operations, such as the request library which is one of the most important aspects of Python having the “delete()” method. It is supported by the “HTTP” and used for deleting the desired resource by sending it as a request.

This post will talk about:

What is a “request.delete()” Method in Python?

To delete/remove particular records, or resource files from the server, the “request.delete()” method can be used in Python. When users directly send a delete request to the server, as a result, the provided link/URL will be deleted, and it will display the success code of the deleted page. It takes some additional arguments and values that are utilized while communicating with the server.

Syntax

Let’s go through the general syntax of the above-stated method:

request.delete(url, params ={'key': 'value'}, args)

 

Here, the above-specified methods take three parameters:

  • url” is the path of the deleted request. It is a required parameter.
  • params” is the list of rows that sends a query string. It is optional.
  • args” is also an optional parameter that can pass several arguments, such as cookies, streams, headers, and many more.

How to Use the “request.delete()” Method in Python?

Check out the provided example to learn how the “request.delete()” method is used for deleting records or resource files.

Example

First, imported the “requests” library. Then, we initialized the “request.delete()” method and passed the URL of our desired record which needed to be deleted. After that, we defined the data at ‘key’ and ‘value’ and passed it to the “delReq” variable. Next, we invoked the “print()” function with the desired message to obtain the success code of deleted record as a result:

import requests

delRep=requests.delete('https://docs.google.com/document/d/1-ekgzC7uFYeOObidl3UAyjSj0zDPp46141Q-8CInpKk/edit', data ={'key':'value'})

print("The Provided Page is Deleted Successfully.",

"The Success Code of Deleted Page is : ", delRep)

 

According to the provided output, the specified deleting request has been processed successfully:

That’s all! We have explained the “request.delete()” method and its usage with an example.

Conclusion

In Python, the “request.delete()” method can be used to delete/remove particular records, or resource files. It takes some additional arguments and values that are utilized while communicating with the server. This post elaborated on the “request.delete()” method and its usage with an example.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.