Support post_data in utils.http.get_url

This commit is contained in:
jesopo 2018-10-08 12:43:31 +01:00
parent fdccba9bf0
commit 383767c7fb

View file

@ -14,20 +14,25 @@ def get_url(url, **kwargs):
method = kwargs.get("method", "GET") method = kwargs.get("method", "GET")
get_params = kwargs.get("get_params", "") get_params = kwargs.get("get_params", "")
post_params = kwargs.get("post_params", None) post_params = kwargs.get("post_params", None)
post_data = kwargs.get("post_data", None)
headers = kwargs.get("headers", {}) headers = kwargs.get("headers", {})
if get_params: if get_params:
get_params = "?%s" % urllib.parse.urlencode(get_params) get_params = "?%s" % urllib.parse.urlencode(get_params)
if post_params: if post_params:
post_params = urllib.parse.urlencode(post_params).encode("utf8") post_data = urllib.parse.urlencode(post_params)
url = "%s%s" % (url, get_params) url = "%s%s" % (url, get_params)
try: try:
url.encode("latin-1") url.encode("latin-1")
if post_data:
post_data = post_data.encode("utf8")
except UnicodeEncodeError: except UnicodeEncodeError:
if kwargs.get("code"): if kwargs.get("code"):
return 0, False return 0, False
return False return False
request = urllib.request.Request(url, post_params) request = urllib.request.Request(url, post_data)
request.add_header("Accept-Language", "en-US") request.add_header("Accept-Language", "en-US")
request.add_header("User-Agent", USER_AGENT) request.add_header("User-Agent", USER_AGENT)
for header, value in headers.items(): for header, value in headers.items():