flask中修改响应的Content-Type

第一种方法:

x = "some data you want to return"
return x, 200, {'Content-Type': 'text/css; charset=utf-8'}

第二种方法:

from flask import Response
r = Response(response="TEST OK", status=200, mimetype="application/xml")
r.headers["Content-Type"] = "text/xml; charset=utf-8"
return r

不建议使用。原因如

If you use Response directly, you lose the chance to customize the responses by setting app.response_class. The make_response method uses the app.responses_class to make the response object

第三种方法:

from flask import Flask, make_response                                      
app = Flask(__name__)

@app.route('/')                    
def index(): 
    bar = '<body>foo</body>'      
    response = make_response(bar)
    response.headers['Content-Type'] = 'text/xml; charset=utf-8'
    return response