Xing Shi Xing Shi - 3 months ago 37
Python Question

How to response a unicode string in flask restful api?

I am using flask.ext.rest to build a api. I want return some chinese string. However, every time I receive

"\u7231"
(This is a string of length 8). What should I do to receive
?

from flask import Flask
from flask.ext.restful import reqparse, abort, Api, Resource
class E2C(Resource): # English to Chinglish
def get(self):
chinese = u'爱'
type(chinese) # unicode
return chinese

Answer

The get method should return a Response instance. see docs here.

The code should be:

from flask import Flask, make_response
from flask.ext.restful import reqparse, abort, Api, Resource
class E2C(Resource): # English to Chinglish
    def get(self):
        chinese = u'爱'
        type(chinese) # unicode
        return make_response(chinese)