From: Michael Tremer Date: Wed, 30 Nov 2016 17:09:48 +0000 (+0100) Subject: http: Convert data argument according to method X-Git-Tag: 0.9.28~1285^2~1455 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4b62e50b157e05b68d0533a5726b6e6651c8dc5e;p=pakfire.git http: Convert data argument according to method data is a dict() with various arguments that are either posted in the body of the request (POST) or added to the URL (GET). For a simple way to send requests, the HTTP client takes care of building a correct request. Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/http.py b/src/pakfire/http.py index 81e88d495..c8460197a 100644 --- a/src/pakfire/http.py +++ b/src/pakfire/http.py @@ -67,7 +67,21 @@ class Client(object): if self.baseurl: url = urllib.parse.urljoin(self.baseurl, url) - req = urllib.request.Request(url) + # Encode data + if data: + data = urllib.parse.urlencode(data) + + # Add data arguments to the URL when using GET + if method == "GET": + url += "?%s" % data + data = None + + # Convert data into Bytes for POST + elif method == "POST": + data = bytes(data, "ascii") + + # Create a request + req = urllib.request.Request(url, data=data) # Add our user agent req.add_header("User-Agent", "pakfire/%s" % PAKFIRE_VERSION) @@ -81,6 +95,11 @@ class Client(object): for protocol, host in self.proxies.items(): req.set_proxy(host, protocol) + # When we send data in a post request, we must set the + # Content-Length header + if data and method == "POST": + req.add_header("Content-Length", len(data)) + # Check if method is correct assert method == req.get_method()