我的 Flask 应用没有正确地 return 非 ascii 字符
My flask app doesn't return non-ascii character properly
我正在尝试使用 flask-restful api,作为返回值,代码应该返回 json 数据列表。但是,当 json 中的内容是非 ascii 字符(èòsèèò)时,返回值
这是示例代码:
#! /usr/bin/env python
# coding: utf-8
from flask import Flask, Response
from flask_restful import Resource, Api
import json
app = Flask(__name__)
# Create the API
API = Api(app)
@app.route('/')
def hello_world():
return Response('Here, with Response it works well: höne')
class APICLASS(Resource):
"""
"""
def get(self, id):
return [
{
"hey": 11,
"test": "höne"
}], 200
API.add_resource(APICLASS, '/<string:id>')
if __name__ == '__main__':
app.run(debug=True)
但是当我在本地主机上检查结果时,我看到以下输出:
[
{
"hey": 11,
"test": "h\u00f6ne"
}]
显然与this bug有关。
我不确定是否有任何副作用,但这可能会有所帮助:
# ...
app = Flask(__name__)
# Create the API
API = Api(app)
API.app.config['RESTFUL_JSON'] = {
'ensure_ascii': False
}
@app.route('/')
# Rest of your code
我正在尝试使用 flask-restful api,作为返回值,代码应该返回 json 数据列表。但是,当 json 中的内容是非 ascii 字符(èòsèèò)时,返回值
这是示例代码:
#! /usr/bin/env python
# coding: utf-8
from flask import Flask, Response
from flask_restful import Resource, Api
import json
app = Flask(__name__)
# Create the API
API = Api(app)
@app.route('/')
def hello_world():
return Response('Here, with Response it works well: höne')
class APICLASS(Resource):
"""
"""
def get(self, id):
return [
{
"hey": 11,
"test": "höne"
}], 200
API.add_resource(APICLASS, '/<string:id>')
if __name__ == '__main__':
app.run(debug=True)
但是当我在本地主机上检查结果时,我看到以下输出:
[
{
"hey": 11,
"test": "h\u00f6ne"
}]
显然与this bug有关。 我不确定是否有任何副作用,但这可能会有所帮助:
# ...
app = Flask(__name__)
# Create the API
API = Api(app)
API.app.config['RESTFUL_JSON'] = {
'ensure_ascii': False
}
@app.route('/')
# Rest of your code