Categories
Python Answers

How to set content type with Python Flask?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *