diff options
author | Jakub Wilk <jwilk@jwilk.net> | 2017-02-08 22:38:05 +0100 |
---|---|---|
committer | Jakub Wilk <jwilk@jwilk.net> | 2017-02-08 22:38:05 +0100 |
commit | 936852fc80e09622a7461d66bd6a46547dbb3eb5 (patch) | |
tree | 991f82ffe9815170b69cc74138b32081981c91f3 | |
parent | d64c789ab2bbdf8ccd63fa819e5c23597d4b7bd0 (diff) | |
download | python-lxml-936852fc80e09622a7461d66bd6a46547dbb3eb5.tar.gz |
Fix POST form submission for Python 3
In Python 3, urlencode() returns Unicode string; but urlopen() wants
only bytes in data, so it need to be encoded first.
Fixes:
>>> form = lxml.html.fromstring('<form method="POST">', base_url='http://localhost')
>>> lxml.html.submit_form(form)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../lxml/html/__init__.py", line 1119, in submit_form
return open_http(form.method, url, values)
File ".../lxml/html/__init__.py", line 1140, in open_http_urllib
return urlopen(url, data)
File ".../urllib/request.py", line 163, in urlopen
return opener.open(url, data, timeout)
File ".../urllib/request.py", line 464, in open
req = meth(req)
File ".../urllib/request.py", line 1183, in do_request_
raise TypeError(msg)
TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str.
-rw-r--r-- | src/lxml/html/__init__.py | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/src/lxml/html/__init__.py b/src/lxml/html/__init__.py index 525f9dc2..b0ab2c1e 100644 --- a/src/lxml/html/__init__.py +++ b/src/lxml/html/__init__.py @@ -1137,6 +1137,8 @@ def open_http_urllib(method, url, values): data = None else: data = urlencode(values) + if not isinstance(data, bytes): + data = data.encode('ASCII') return urlopen(url, data) |