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

UTF-8 char garbled in json response by frontend #21

Open
hyzeer opened this issue Aug 2, 2019 · 2 comments
Open

UTF-8 char garbled in json response by frontend #21

hyzeer opened this issue Aug 2, 2019 · 2 comments

Comments

@hyzeer
Copy link

hyzeer commented Aug 2, 2019

Hi, thanks for your new firmware, it can handle UTF-8 char properly, but it has new problem about garbling. my codes is the following:

@websvr.resource('/config/wifi/aplist', method='GET')
async def connect_to_ap(data):
    gc.collect()
    sta = network.WLAN(network.STA_IF)
    sta.active(True)
    ap_list = sta.scan()
    resp = json.dumps({
        "msg": "OK",
        "result": [
            {"ssid": x[0].decode(), "authmode": x[4], "rssi": x[3]}
            for x in ap_list
        ]
    })
    for s in resp:
        yield s

the above codes works correctly if values of resp['result'] contains no UTF-8 chars, otherwise the front end would receive unreadable code, like this:
图片

I guess this due to Content-Type field should not be 'application/json;charset=utf-8' in Response-Header, but I can't debug it..

@belyalov
Copy link
Owner

belyalov commented Aug 5, 2019

Hi,

I think we can add override for encoding, let me try it (however it will take time, busy days)

@hyzeer
Copy link
Author

hyzeer commented Aug 6, 2019

Thanks for your reply in busy days. :)

another point, at v1.3.3, chars length handle error, I did this to avoiding the error:

 for x in ap_list:
      difference = len(x[0].decode().encode('utf-8')) - len(x[0].decode())
      yield '"ssid": "{}",'.format(x[0].decode()) + 'a' * difference

I used the above codes, utf-8 chars can be handled propely by tinyweb and decoded correctly by the browser. I don't know why it happened. 🤣

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