How to make delete / put request in python

I can make a request to receive or send using urllib, but how can I make DELETE and PUT requests?

+3
source share
7 answers

PUT request can be made using httplib2

http://code.google.com/p/httplib2

+1
source

The requests library can handle POST, PUT, DELETE and all other HTTP methods and is much less scary than urllib, httplib and their variants.

+7
source

get_method :

def _make_request(url, data, method):
    request.urllib2.Request(url, data=data)
    request.get_method = lambda: method

DELETE.

.

+4

urlopen

POST.

urllib.request.urlopen(url, data=None[, timeout])

, DELETE HTTP urlib - :

Request.get_method()
HTTP-. HTTP , "GET" "POST".

httplib, httplib2, Twisted .for HTTP.

+1

As far as I know, urllib and urllib2 only support queries GETand POST. You should probably take a look at httplib or httplib2 .

+1
source

The default HTTP methods in the urllib library are: POST and GET:

def get_method(self):
    """Return a string indicating the HTTP request method."""
    default_method = "POST" if self.data is not None else "GET"
    return getattr(self, 'method', default_method)

But we can override this get_method () function to get a DELETE request:

req = urllib.request.Request(new_url)
req.get_method = lambda: "DELETE"
0
source

Source: https://habr.com/ru/post/1753083/


All Articles