Python Flask, how to set content type
--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Underwater World
--
Chapters
00:00 Python Flask, How To Set Content Type
00:15 Accepted Answer Score 344
00:35 Answer 2 Score 54
01:02 Answer 3 Score 191
01:30 Answer 4 Score 23
01:38 Thank you
--
Full question
https://stackoverflow.com/questions/1177...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #flask
#avk47
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Underwater World
--
Chapters
00:00 Python Flask, How To Set Content Type
00:15 Accepted Answer Score 344
00:35 Answer 2 Score 54
01:02 Answer 3 Score 191
01:30 Answer 4 Score 23
01:38 Thank you
--
Full question
https://stackoverflow.com/questions/1177...
--
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