Python Flask, how to set content type

前端 未结 7 1601
走了就别回头了
走了就别回头了 2020-12-02 05:10

I am using Flask and I return an XML file from a get request. How do I set the content type to xml ?

e.g.

@app.route(\'/ajax_ddl\')
def ajax_ddl():
          


        
相关标签:
7条回答
  • 2020-12-02 06:13

    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.

    0 讨论(0)
提交回复
热议问题