我正在使用Flask,並從get請求中返回一個XML文件。我如何設置內容類型?Python燒瓶,如何設置內容類型
例如
@app.route('/ajax_ddl')
def ajax_ddl():
xml = 'foo'
header("Content-type: text/xml")
return xml
我正在使用Flask,並從get請求中返回一個XML文件。我如何設置內容類型?Python燒瓶,如何設置內容類型
例如
@app.route('/ajax_ddl')
def ajax_ddl():
xml = 'foo'
header("Content-type: text/xml")
return xml
嘗試這樣的:
from flask import Response
@app.route('/ajax_ddl')
def ajax_ddl():
xml = 'foo'
return Response(xml, mimetype='text/xml')
實際內容類型是基於mimeType參數和字符集(默認爲UTF-8)。
響應(和要求)對象記錄在這裏:http://werkzeug.pocoo.org/docs/wrappers/
我喜歡和upvoted @Simon Sapin條款的答案。最後我採取了略微不同的策略,但是,創建了自己的裝飾:
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
,因此使用它:
@app.route('/ajax_ddl')
@returns_xml
def ajax_ddl():
xml = 'foo'
return xml
我覺得這是稍微更舒適。
像這樣簡單
x = "some data you want to return"
return x, 200, {'Content-Type': 'text/css; charset=utf-8'}
希望它可以幫助
更新: 使用此方法,因爲它會與這兩個蟒蛇2.x和Python 3.x都有
,其次它的工作也消除了多頭問題。
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
from flask import Flask, render_template, make_response
app = Flask(__name__)
@app.route('/user/xml')
def user_xml():
resp = make_response(render_template('xml/user.html', username='Ryan'))
resp.headers['Content-type'] = 'text/xml; charset=utf-8'
return resp
我認爲這個答案很重要,因爲它明確瞭如何從render_template改變某些東西的標題。 – 2017-04-21 20:36:49
使用make_response method讓您的數據的響應。然後設置mimetype attribute。最後返回此響應:
@app.route('/ajax_ddl')
def ajax_ddl():
xml = 'foo'
resp = app.make_response(xml)
resp.mimetype = "text/xml"
return resp
如果你直接使用Response
,你失去了通過設置app.response_class
以自定義響應的機會。 make_response
方法使用app.responses_class
來生成響應對象。在此,你可以創建自己的類,添加使您的應用程序在全球使用它:
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
這實質上是@ SimonSapin接受的答案重新包裝。 – J0e3gan 2015-07-31 17:35:56
@ J0e3gan謝謝。我擴大了我的答案,以更好地解釋爲什麼使用'make_response'比使用'Response'更好 – 2015-07-31 21:17:21
通常你不必創建Response
反對自己,因爲make_response()
會照顧的,對於你。
from flask import Flask, make_response
app = Flask(__name__)
@app.route('/')
def index():
bar = '<body>foo</body>'
response = make_response(bar)
response.headers['Content-Type'] = 'text/xml; charset=utf-8'
return response
一兩件事,似乎沒有人提到的after_this_request
,我想說點什麼:
這個請求後執行的函數。這對修改響應對象很有用。該函數傳遞響應對象,並且必須返回相同的或新的。
,所以我們可以用after_this_request
做到這一點,代碼應該是這樣的:
from flask import Flask, after_this_request
app = Flask(__name__)
@app.route('/')
def index():
@after_this_request
def add_header(response):
response.headers['Content-Type'] = 'text/xml; charset=utf-8'
return response
return '<body>foobar</body>'
是否可以設置這些和其他選項在全球範圍內(即:默認)? – earthmeLon 2013-07-29 19:02:50
@earthmeLon,做一個'flask.Response'的子類,覆蓋'default_mimetype'類屬性,並將其設置爲'app.response_class' http://werkzeug.pocoo.org/docs/wrappers/#werkzeug.wrappers .BaseResponse.default_mimetype http://flask.pocoo.org/docs/api/#flask.Flask.response_class – 2013-07-30 08:42:22
@earthmeLon:如果你像西蒙指出的那樣設置'app.response_class',記得使用'app.make_response'讓你的響應實例像[在下面的答案中指出](http://stackoverflow.com/a/31751634/110204)。 – 2015-08-05 15:52:30