234

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():
    xml = 'foo'
    header("Content-type: text/xml")
    return xml

8 Answers 8

330

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/

4
  • 2
    Is it possible to set these and other options on a global level (ie: default)?
    – earthmeLon
    Jul 29, 2013 at 19:02
  • 12
    @earthmeLon, make a subclass of flask.Response, override the default_mimetype class attribute, and set that as app.response_class werkzeug.pocoo.org/docs/wrappers/… flask.pocoo.org/docs/api/#flask.Flask.response_class Jul 30, 2013 at 8:42
  • @earthmeLon: If you set app.response_class like Simon points out, remember to use app.make_response to get your reponse instance like pointed out in the answer below. Aug 5, 2015 at 15:52
  • Requests with browsers or postman work fine with this approach, however curl does not work well with the returned Response object. Curl will just print "Found". With curl "return content, status_code, header" seem to work better.
    – fuma
    Dec 21, 2018 at 9:35
185

As simple as this

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

Update: Use the method below because it will work with both python 2.x and python 3.x and it eliminates the "multiple header" problem (potentially emitting multiple, duplicate headers).

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
6
  • 16
    The simplest solution. Definitely should be the accepted answer
    – Omer Dagan
    May 18, 2015 at 14:05
  • There is a drawback: it allows you only ADD headers. When I did it, I ended up with two Content-Type headers in response - default one and added one.
    – omikron
    Feb 2, 2017 at 16:15
  • 2
    @omikron I have updated the answer, try the new method it should work. Feb 4, 2017 at 16:52
  • 2
    @HarshDaftary it is unclear to me which is the new method and which is the old. Could you please replace "this method" with "the method above" or "the method below"?
    – Leo
    Nov 1, 2020 at 1:08
  • 1
    @Leo I added the desired clarification. Sep 2, 2021 at 21:12
51

I like and upvoted @Simon Sapin's answer. I ended up taking a slightly different tack, however, and created my own decorator:

from flask import Response
from functools import wraps

def returns_xml(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        r = f(*args, **kwargs)
        return Response(r, content_type='text/xml; charset=utf-8')
    return decorated_function

and use it thus:

@app.route('/ajax_ddl')
@returns_xml
def ajax_ddl():
    xml = 'foo'
    return xml

I think this is slightly more comfortable.

1
  • 4
    When returning both a response and a status code like return 'msg', 200, this will lead to ValueError: Expected bytes. Instead, change the decorator to return Response(*r, content_type='whatever'). It will unpack the tuple to arguments. Thank you though, for an elegant solution!
    – Felix
    May 28, 2019 at 6:11
30

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  
2
  • This is essentially @SimonSapin's accepted answer repackaged.
    – J0e3gan
    Jul 31, 2015 at 17:35
  • 1
    @J0e3gan thanks. I have expanded my answer to better explain why using make_response is better than using Response Jul 31, 2015 at 21:17
20
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
1
  • 2
    I think this answer is important because it makes clear how to change the headers on something from a render_template. Apr 21, 2017 at 20:36
10

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)
5

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>'
1

When sending files

from flask import send_file

@app.route("/graph", methods = ['GET'])
def grafh():
    return send_file('graph.png', mimetype='image/png', as_attachment=False)

Change as_attachment if you want it previewed or as download

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.