Open In App

DELETE method- Python requests

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Requests library is one of the important aspects of Python for making HTTP requests to a specified URL. This article revolves around how one can make DELETE request to a specified URL using requests.delete() method. Before checking out the DELETE method, let’s figure out what a Http DELETE request is –

DELETE Http Method

DELETE is a request method supported by HTTP used by the World Wide Web. The DELETE method deletes the specified resource. As with a PUT request, you need to specify a particular resource for this operation. A successful response SHOULD be 200 (OK) if the response includes an entity describing the status, 202 (Accepted) if the action has not yet been enacted, or 204 (No Content) if the action has been enacted but the response does not include an entity.
An example URI looks like for delete operation

http://www.example.com/articles/12345

How to make DELETE request through Python Requests

Python’s requests module provides in-built method called delete() for making a DELETE request to a specified URI.

Syntax –

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

Example –

Let’s try making a request to httpbin’s APIs for example purposes.




import requests
  
# Making a DELETE request
r = requests.delete('https://httpbin.org / delete', data ={'key':'value'})
  
# check status code for response received
# success code - 200
print(r)
  
# print content of request
print(r.json())


save this file as request.py and through terminal run,

python request.py

Output –

delete-method-python-requests

Advanced with DELETE request

The DELETE method requests that the origin server delete the resource identified by the Request-URI. This method MAY be overridden by human intervention (or other means) on the origin server. The client cannot be guaranteed that the operation has been carried out, even if the status code returned from the origin server indicates that the action has been completed successfully. However, the server SHOULD NOT indicate success unless, at the time the response is given, it intends to delete the resource or move it to an inaccessible location.


Last Updated : 26 Feb, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads