问 题
下面的示例代码,运行在 Python 2.7 环境
pip install flask-restful flask
# -*- coding:utf-8 -*-
from flask import Flask
from flask_restful import Api, Resource, reqparse, marshal_with, fields
class Greetings(Resource):
def get(self, message=None):
message = '中文한국어'
greeting = 'You have received a message: {0}'.format(message) if message else 'zzzzzzz......'
return greeting, 200
app = Flask(__name__)
api = Api(app)
api.add_resource(Greetings, '/radio/', '/radio/')
if __name__ == '__main__':
app.run(port=8000,debug=True)
这是运行的结果:
? Discuq@iDocker: ~ => curl http://127.0.0.1:8000/radio/
"You have received a message: \u4e2d\u6587\ud55c\uad6d\uc5b4"
期望得到的结果是:
? Discuq@iDocker: ~ => curl http://127.0.0.1:8000/radio/
"You have received a message: 中文한국어"
解决方案
指定 RESTFUL_JSON 配置项:
app = Flask(__name__)
app.config.update(RESTFUL_JSON=dict(ensure_ascii=False))
扫一扫关注IT屋
微信公众号搜索 “ IT屋 ” ,选择关注与百万开发者在一起