From: W. Trevor King Date: Fri, 24 Aug 2012 13:29:58 +0000 (-0400) Subject: util:http: add ability to pass raw POST data with get_post_url. X-Git-Tag: 1.1.0~129 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=0cd072b9710ee964e6f449abd9265d85e02f34d2;p=be.git util:http: add ability to pass raw POST data with get_post_url. --- diff --git a/libbe/util/http.py b/libbe/util/http.py index b9b1765..8af97eb 100644 --- a/libbe/util/http.py +++ b/libbe/util/http.py @@ -49,7 +49,8 @@ class HTTPError (Exception): return self.msg -def get_post_url(url, get=True, data_dict=None, headers=[], agent=None): +def get_post_url(url, get=True, data=None, data_dict=None, headers=[], + agent=None): """Execute a GET or POST transaction. Parameters @@ -58,25 +59,31 @@ def get_post_url(url, get=True, data_dict=None, headers=[], agent=None): The base URL (query portion added internally, if necessary). get : bool Use GET if True, otherwise use POST. + data : str + Raw data to send by POST (requires POST). data_dict : dict Data to send, either by URL query (if GET) or by POST (if POST). + Cannot be given in combination with `data`. headers : list Extra HTTP headers to add to the request. agent : str User agent string overriding the BE default. """ - if data_dict is None: - data_dict = {} if agent is None: agent = USER_AGENT - if get is True: - if data_dict != {}: - # encode get parameters in the url - param_string = urllib.urlencode(data_dict) - url = '{}?{}'.format(url, param_string) - data = None + if data is None: + if data_dict is None: + data_dict = {} + if get is True: + if data_dict != {}: + # encode get parameters in the url + param_string = urllib.urlencode(data_dict) + url = '{}?{}'.format(url, param_string) + else: + data = urllib.urlencode(data_dict) else: - data = urllib.urlencode(data_dict) + assert get is False, (data, get) + assert data_dict is None, (data, data_dict) headers = dict(headers) headers['User-Agent'] = agent req = urllib2.Request(url, data=data, headers=headers)