diff options
author | W. Trevor King <wking@tremily.us> | 2012-08-24 09:29:58 -0400 |
---|---|---|
committer | W. Trevor King <wking@tremily.us> | 2012-08-24 09:32:47 -0400 |
commit | 0cd072b9710ee964e6f449abd9265d85e02f34d2 (patch) | |
tree | 2636f968e25d1b80c99513ee278ba6a46f2ac7b5 | |
parent | f0fe11190de73c153d7fb66734434ade4a21dbc2 (diff) | |
download | bugseverywhere-0cd072b9710ee964e6f449abd9265d85e02f34d2.tar.gz |
util:http: add ability to pass raw POST data with get_post_url.
-rw-r--r-- | libbe/util/http.py | 27 |
1 files changed, 17 insertions, 10 deletions
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) |