micropython / micropython-lib

Core Python libraries ported to MicroPython

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

alx696 opened this issue · comments

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

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.

Thanks!