Bootstrap FreeKB - Python (Scripting) - DELETE request
Python (Scripting) - DELETE request

Updated:   |  Python (Scripting) articles

The requests module can be used to issue a REST API request.

Here is how you can make a DELETE request.

#!/usr/bin/python3
import requests

try:
  response = requests.delete(
    "http://www.example.com/api/example"
  )
except requests.exceptions.Timeout:
  print(f"DELETE request timed out")
except Exception as exception:
  print(f"requests.delete raised the following exception: {exception}")
else:
  print("requests.delete appears to have been successful")

 

verify=False can be used to ignore SSL issues, such as when using a self-signed certificate. This is like using the --insecure flag in curl.

response = requests.delete("http://www.example.com/api/example", verify=False)

 

Here is how you can include a username and password if basic authentication is required.

#!/usr/bin/python3
import requests

url       = "http://www.example.com/api/example"
session   = requests.Session()

try:
  session.auth = ("john.doe", "itsasecret")
except Exception as exception:
  print(f"session.auth raised the following exception: {exception}")
else:
  print("session.auth appears to have been successful")

try:
  response = session.delete(url)
except requests.exceptions.Timeout:
  print(f"DELETE request timed out")
except Exception as exception:
  print(f"session.delete raised the following exception: {exception}")
else:
  print("session.delete appears to have been successful")

 

There are a number of different objects that can be used to return useful information.

print(f"response             = {response}")
print(f"response.status_code = {response.status_code}")
print(f"response.ok          = {response.ok}")
print(f"response.content     = {response.content}")
print(f"response.text        = {response.text}")
print(f"response.json        = {response.json()}")
print(f"response.headers     = {response.headers}")

 

Since each API has it's own unique response, the response you get back will different between APIs. However, I typically expect to get some sort of response that let's me know if the request was successful or if it failed.

response             = <Response [200]>
response.status_code = 200
response.ok          = True
response.content     = {"result":{"success":true}}
response.text        = {"result":{"success":true}}
response.json        = {u'result': {u'success': True}}

 

If some issue occurs, you may want to set logging to DEBUG to view the full request and response.

#!/usr/bin/python
import logging

try:
    import http.client as http_client
except ImportError:
    import httplib as http_client

http_client.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 075c6c in the box below so that we can be sure you are a human.