Sometimes, we want to set content type with Python Flask.
In this article, we’ll look at how to set content type with Python Flask.
How to set content type with Python Flask?
To set content type with Python Flask, we can create a Response
instance with the mimetype
argument.
For instance, we write
from flask import Response
@app.route('/ajax')
def ajax():
xml = 'foo'
return Response(xml, mimetype='text/xml')
to create the ajax
view that returns a response by creating a Response
instance with the mimetyp
set to 'text/xml'
to return an XML response with the xml
string as the body.
Conclusion
To set content type with Python Flask, we can create a Response
instance with the mimetype
argument.