Python Flask, how to set content type

前端 未结 7 1617
走了就别回头了
走了就别回头了 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 05:57

    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 this method because it will work with both python 2.x and python 3.x

    and secondly it also eliminates multiple header problem.

    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
    

提交回复
热议问题