Skip to content

python-ecosys/requests can not post Chinese, why not add ; charset=UTF-8 to Content-Type? #859

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

Closed
alx696 opened this issue May 16, 2024 · 2 comments

Comments

@alx696
Copy link

alx696 commented May 16, 2024

Must call .encode('utf-8') if body text contains Chinese:

import requests, json

json_text = json.dumps({"id":"test", "name":"esp32-中文-1"}).encode('utf-8')
response = requests.post('https://mc-th.alx6963445.workers.dev/device', headers = {'X-Custom-UUID': 'test', 'X-Custom-TOKEN':'test'}, data = json_text)
print(response.status_code, response.text)
response.close()

If not call .encode('utf-8') , workers can not receive full body text. The content will be garbled.

Will the problem be solved after setting ; charset=UTF-8 to Content-Type ? https://github.com/micropython/micropython-lib/blob/master/python-ecosys/requests/requests/__init__.py#L110

@felixdoerre
Copy link
Contributor

The code example that you provided will not use line 110, so I believe changing things there will not make a difference.

I actually am not sure how .encode('utf-8') could change the values that are sent by the mpy device in this example. Micropython does not even provide any other character encoding. Also, searching various sources, it seems that application/json does not have a charset=-parameter, as json content must be encoded as utf-8 anyways.

Did you store your source file in utf-8? If in doubt, you can try with this string: "esp32-\u4e2d\u6587-1", that makes sure that the string content is correct and ensures that there can be no encoding problem with the source file.

@alx696
Copy link
Author

alx696 commented Jun 7, 2024

Thanks!

@alx696 alx696 closed this as completed Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants