Python Flask, how to set content type

前端 未结 7 1579
走了就别回头了
走了就别回头了 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:03

    You can try the following method(python3.6.2):

    case one:

    @app.route('/hello')
    def hello():
    
        headers={ 'content-type':'text/plain' ,'location':'http://www.stackoverflow'}
        response = make_response('

    hello world

    ',301) response.headers = headers return response

    case two:

    @app.route('/hello')
    def hello():
    
        headers={ 'content-type':'text/plain' ,'location':'http://www.stackoverflow.com'}
        return '

    hello world

    ',301,headers

    I am using Flask .And if you want to return json,you can write this:

    import json # 
    @app.route('/search/')
    def search(keyword):
    
        result = Book.search_by_keyword(keyword)
        return json.dumps(result),200,{'content-type':'application/json'}
    
    
    from flask import jsonify
    @app.route('/search/')
    def search(keyword):
    
        result = Book.search_by_keyword(keyword)
        return jsonify(result)
    

提交回复
热议问题