How to correctly produce and consume UTF-8 json in Python3? (with flask and requests) How to correctly produce and consume UTF-8 json in Python3? (with flask and requests) flask flask

How to correctly produce and consume UTF-8 json in Python3? (with flask and requests)


You can fix the way you make the request by encoding the data object :

my_request = requests.post('http://localhost:5000/reply',                            headers=HEADERS,                            data=DATA.encode('utf-8'))#>>> ít wórked with óíá!

If you add a try/except statement in the app, it returns :

try:    params = request.jsonexcept Exception as e:    params = None    print(e)

400 Bad Request: Failed to decode JSON object: 'utf-8' codec can't decode byte 0xf3 in position 14: invalid continuation byte

You could use this pattern to assign a default value for param


I have not tried the code but may be setting

app.config['JSON_AS_ASCII'] = False

may help.