Home  >  Q&A  >  body text

python - The response returned by flask-restful Chinese becomes unicode literal

The following sample code runs in Python 2.7 environment

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/<message>')


if __name__ == '__main__':
    app.run(port=8000,debug=True)

This is the result of running:

我想大声告诉你我想大声告诉你2711 days ago797

reply all(1)I'll reply

  • 为情所困

    为情所困2017-05-18 10:59:32

    Specify RESTFUL_JSON configuration item:

    app = Flask(__name__)
    app.config.update(RESTFUL_JSON=dict(ensure_ascii=False))

    reply
    0
  • Cancelreply