Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Flask, how to set content type

Tags:

python

flask

People also ask

How do I change header responses in Flask?

To set response headers in Flask and Python, we set the headers property of the response object. to call make_response to create response object that returns a string response. Finally, we return the resp object in the home route.

How do I return an XML response in Flask?

If you want the response in XML format then add Accept/(application/xml) key/value pair in the headers. For POST request I am not doing with the input data, just returning in the response. You will get response with the name passed as a path parameter. The default format is JSON.

What is Make_response Flask?

make_response is a function in the flask. helpers module of the Flask web framework. make_response is for adding additional HTTP headers to a response within a view's code.


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/


As simple as this

x = "some data you want to return"
return x, 200, {'Content-Type': 'text/css; charset=utf-8'}

Hope it helps

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

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.


Use the make_response method to get a response with your data. Then set the mimetype attribute. Finally return this response:

@app.route('/ajax_ddl')
def ajax_ddl():
    xml = 'foo'
    resp = app.make_response(xml)
    resp.mimetype = "text/xml"
    return resp

If you use Response directly, you lose the chance to customize the responses by setting app.response_class. The make_response method uses the app.responses_class to make the response object. In this you can create your own class, add make your application uses it globally:

class MyResponse(app.response_class):
    def __init__(self, *args, **kwargs):
        super(MyResponse, self).__init__(*args, **kwargs)
        self.set_cookie("last-visit", time.ctime())

app.response_class = MyResponse