I am using Flask and I return an XML file from a get request. How do I set the content type to xml ?
e.g.
@app.route(\'/ajax_ddl\')
def ajax_ddl():
Use the make_response method to get a response with your data. Then set the mimetype attribute. Finally return this response:
@app.route('/ajax_ddl')
def ajax_ddl():
xml = 'foo'
resp = app.make_response(xml)
resp.mimetype = "text/xml"
return resp
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. In this you can create your own class, add make your application uses it globally:
class MyResponse(app.response_class):
def __init__(self, *args, **kwargs):
super(MyResponse, self).__init__(*args, **kwargs)
self.set_cookie("last-visit", time.ctime())
app.response_class = MyResponse
Try like this:
from flask import Response
@app.route('/ajax_ddl')
def ajax_ddl():
xml = 'foo'
return Response(xml, mimetype='text/xml')
The actual Content-Type is based on the mimetype parameter and the charset (defaults to UTF-8).
Response (and request) objects are documented here: http://werkzeug.pocoo.org/docs/wrappers/
As simple as this
x = "some data you want to return"
return x, 200, {'Content-Type': 'text/css; charset=utf-8'}
Hope it helps
Update: Use this method because it will work with both python 2.x and python 3.x
and secondly it also eliminates multiple header problem.
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
You can try the following method(python3.6.2):
case one:
@app.route('/hello')
def hello():
headers={ 'content-type':'text/plain' ,'location':'http://www.stackoverflow'}
response = make_response('<h1>hello world</h1>',301)
response.headers = headers
return response
case two:
@app.route('/hello')
def hello():
headers={ 'content-type':'text/plain' ,'location':'http://www.stackoverflow.com'}
return '<h1>hello world</h1>',301,headers
I am using Flask .And if you want to return json,you can write this:
import json #
@app.route('/search/<keyword>')
def search(keyword):
result = Book.search_by_keyword(keyword)
return json.dumps(result),200,{'content-type':'application/json'}
from flask import jsonify
@app.route('/search/<keyword>')
def search(keyword):
result = Book.search_by_keyword(keyword)
return jsonify(result)
Usually you don’t have to create the Response object yourself because make_response() will take care of that for you.
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
One more thing, it seems that no one mentioned the after_this_request, I want to say something:
after_this_request
Executes a function after this request. This is useful to modify response objects. The function is passed the response object and has to return the same or a new one.
so we can do it with after_this_request, the code should look like this:
from flask import Flask, after_this_request
app = Flask(__name__)
@app.route('/')
def index():
@after_this_request
def add_header(response):
response.headers['Content-Type'] = 'text/xml; charset=utf-8'
return response
return '<body>foobar</body>'
from flask import Flask, render_template, make_response
app = Flask(__name__)
@app.route('/user/xml')
def user_xml():
resp = make_response(render_template('xml/user.html', username='Ryan'))
resp.headers['Content-type'] = 'text/xml; charset=utf-8'
return resp