Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request.GetPostData() throws UnicodeEncodeError #517

Closed
cztomczak opened this issue Mar 8, 2019 · 1 comment
Closed

Request.GetPostData() throws UnicodeEncodeError #517

cztomczak opened this issue Mar 8, 2019 · 1 comment

Comments

@cztomczak
Copy link
Owner

cztomczak commented Mar 8, 2019

Here is the faulty code:

retUrlEncoded.update(urlparse.parse_qsl(qs=pyData,

It occurs only when content-type is "application/x-www-form-urlencoded". urllib.parse.parse_qsl requires str data instead of bytes when data contains non-ascii character.

Reported on the Forum:
https://groups.google.com/d/topic/cefpython/A7gDz3XmK1I/discussion

Code to reproduce the problem:

# coding=utf8
from cefpython3 import cefpython as cef
import sys

html = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
  window.onload = function() {
   fetch('http://127.0.0.1:8000', {
       method: 'POST',
       headers: {'Content-Type': 'application/x-www-form-urlencoded'},
       body: 'key=' + encodeURI('🍣'),
   }).then().catch();
  }
</script>
</body>
</html>
"""


class RequestHandler:
    def GetResourceHandler(self, browser, frame, request):
        print(request.GetPostData())
        return None

def main():
    sys.excepthook = cef.ExceptHook
    cef.Initialize()
    browser = cef.CreateBrowserSync(url=cef.GetDataUrl(html))
    browser.SetClientHandler(RequestHandler())
    cef.MessageLoop()
    del browser
    cef.Shutdown()


if __name__ == '__main__':
    main()

Error and traceback:

Traceback (most recent call last):
  File "request_handler.pyx", line 150, in cefpython_py37.RequestHandler_GetResourceHandler
  File "/Volumes/SSD-PLU3/Users/*****/Library/Preferences/PyCharm2018.3/scratches/getpost.py", line 28, in GetResourceHandler
    print(request.GetPostData())
  File "request.pyx", line 83, in cefpython_py37.PyRequest.GetPostData
  File "request.pyx", line 122, in cefpython_py37.PyRequest.GetPostData
  File "/Users/******/.pyenv/versions/3.7.2/Python.framework/Versions/3.7/lib/python3.7/urllib/parse.py", line 720, in parse_qsl
    value = _coerce_result(value)
  File "/Users/******/.pyenv/versions/3.7.2/Python.framework/Versions/3.7/lib/python3.7/urllib/parse.py", line 103, in _encode_result
    return obj.encode(encoding, errors)
UnicodeEncodeError: 'ascii' codec can't encode character '\U0001f363' in position 0: ordinal not in range(128)
@cztomczak
Copy link
Owner Author

Fixed in commit 8f1484c.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant