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 use the Response class.

For instance, we write

from flask import Response


@app.route("/ajax_ddl")
def ajax_ddl():
    xml = "foo"
    return Response(xml, mimetype="text/xml")

to create a Response object with the xml string as the body and the mimetype set to the MIME type of the response.

Conclusion

To set content type with Python Flask, we can use the Response class.

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 *