← index #516Issue #859
Related · high · value 1.096
QUERY · ISSUE

Lib json ensure_acsii parameters can be set

openby Zero688opened 2022-08-08updated 2025-08-26
needs-infounicode

Now json.dumps(str) turn Chinese character Json object with UTF8 . But send requests need Unicode , Could add ensure_acsii parameters ? or Any other solution?

2 comments
Zero688 · 2022-08-10

Not a character set issue

jonnor · 2024-08-25

Hi and thank you for the report. It is not entirely clear what the issue is. Is this still a problem? If so, can you provide a minimal code example which shows the issue?

CANDIDATE · ISSUE

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

closedby shiwaimanongopened 2024-05-16updated 2024-06-07

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

2 comments
felixdoerre · 2024-05-31

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.

shiwaimanong · 2024-06-07

Thanks!

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied