Python Flask, how to set content type
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Riding Sky Waves v001
--
Chapters
00:00 Question
00:27 Accepted answer (Score 329)
00:54 Answer 2 (Score 183)
01:26 Answer 3 (Score 51)
02:01 Answer 4 (Score 30)
02:56 Thank you
--
Full question
https://stackoverflow.com/questions/1177...
Accepted answer links:
http://werkzeug.pocoo.org/docs/wrappers/
Answer 4 links:
[make_response method]: https://flask.palletsprojects.com/en/1.1...
[mimetype attribute]: https://flask.palletsprojects.com/en/1.1...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #flask
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Riding Sky Waves v001
--
Chapters
00:00 Question
00:27 Accepted answer (Score 329)
00:54 Answer 2 (Score 183)
01:26 Answer 3 (Score 51)
02:01 Answer 4 (Score 30)
02:56 Thank you
--
Full question
https://stackoverflow.com/questions/1177...
Accepted answer links:
http://werkzeug.pocoo.org/docs/wrappers/
Answer 4 links:
[make_response method]: https://flask.palletsprojects.com/en/1.1...
[mimetype attribute]: https://flask.palletsprojects.com/en/1.1...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #flask
#avk47
ACCEPTED ANSWER
Score 344
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/
ANSWER 2
Score 191
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
ANSWER 3
Score 54
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.
ANSWER 4
Score 23
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