Reputation: 139
I use Flask to run a RESTful service and have discovered, that Unicode characters are not being passed correctly, when values are passed as parameters.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
...
@app.route('/search/<query>')
def Search(query):
result = { "result": query }
return jsonify(result), 200
When I call the service using a REST client with URL ...
http://localhost/search/zürich
... the output looks like this:
{
"result": "z\ufffdrich"
}
When hard coding the query string like this:
@app.route('/search/<query>')
def Search(query):
result = { "result": "Zürich" }
return jsonify(result), 200
... the output is correctly encoded:
{
"result": "Zürich"
}
So I digged in the Flask config options settings and have set the options parameter 'JSON_AS_ASCII' to false.
By default Flask serialize object to ascii-encoded JSON. If this is set to False Flask will not encode to ASCII and output strings as-is and return unicode strings. jsonify will automatically encode it in utf-8 then for transport for instance.
Is that a bug in Flask or did I miss anything in the Flask configuration setting?
Upvotes: 1
Views: 1726
Reputation: 5463
This documentation means that if
app.config['JSON_AS_ASCII'] = False
The output will be in unicode.
So it should be set to True
to get the output in ASCII, which is what you want.
Upvotes: 1