make a http delete request in python

To make an HTTP DELETE request in Python, you can use the urllib library. Specifically, you can use the urllib.request module's urlopen() function to send a DELETE request to a specified URL. Here is an example code block showing how to do this:

main.py
import urllib.request

url = 'http://example.com/api/some/resource?id=123'

req = urllib.request.Request(url=url, method='DELETE')
with urllib.request.urlopen(req) as f:
    response = f.read().decode('utf-8')

print(response)
227 chars
10 lines

In this code, we first define the URL that we want to send the DELETE request to. Then we create a Request object using that URL and specifying the HTTP method as 'DELETE'. We then use the urlopen() function to send the DELETE request and retrieve the response. Finally, we decode the response using the UTF-8 encoding and print it.

gistlibby LogSnag