我正在使用Flask,我从get请求返回一个XML文件.如何设置内容类型?
例如
@app.route('/ajax_ddl')
def ajax_ddl():
xml = 'foo'
header("Content-type: text/xml")
return xml
Run Code Online (Sandbox Code Playgroud)
Sim*_*pin 224
试试这样:
from flask import Response
@app.route('/ajax_ddl')
def ajax_ddl():
xml = 'foo'
return Response(xml, mimetype='text/xml')
Run Code Online (Sandbox Code Playgroud)
实际的Content-Type基于mimetype参数和charset(默认为UTF-8).
响应(和请求)对象在此处记录:http://werkzeug.pocoo.org/docs/wrappers/
Har*_*ary 129
就这么简单
x = "some data you want to return"
return x, 200, {'Content-Type': 'text/css; charset=utf-8'}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你
更新:使用此方法,因为它将适用于python 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
Run Code Online (Sandbox Code Playgroud)
Mic*_*olf 43
我喜欢并赞成@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
Run Code Online (Sandbox Code Playgroud)
并使用它:
@app.route('/ajax_ddl')
@returns_xml
def ajax_ddl():
xml = 'foo'
return xml
Run Code Online (Sandbox Code Playgroud)
我认为这稍微舒服一些.
Mar*_*llo 23
使用make_response方法获取数据响应.然后设置mimetype属性.最后回复此回复:
@app.route('/ajax_ddl')
def ajax_ddl():
xml = 'foo'
resp = app.make_response(xml)
resp.mimetype = "text/xml"
return resp
Run Code Online (Sandbox Code Playgroud)
如果Response直接使用,则无法通过设置自定义响应app.response_class.该make_response方法使用app.responses_class来制作响应对象.在这里你可以创建自己的类,添加make你的应用程序全局使用它:
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
Run Code Online (Sandbox Code Playgroud)
小智 13
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
Run Code Online (Sandbox Code Playgroud)
你可以试试下面的方法(python3.6.2)?
案例一?
@app.route('/hello')
def hello():
headers={ 'content-type':'text/plain' ,'location':'http://www.stackoverflow'}
response = make_response('<h1>hello world</h1>',301)
response.headers = headers
return response
Run Code Online (Sandbox Code Playgroud)
情况二?
@app.route('/hello')
def hello():
headers={ 'content-type':'text/plain' ,'location':'http://www.stackoverflow.com'}
return '<h1>hello world</h1>',301,headers
Run Code Online (Sandbox Code Playgroud)
我正在使用 Flask 。如果你想返回 json,你可以这样写:
import json #
@app.route('/search/<keyword>')
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/<keyword>')
def search(keyword):
result = Book.search_by_keyword(keyword)
return jsonify(result)
Run Code Online (Sandbox Code Playgroud)
通常您不必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
Run Code Online (Sandbox Code Playgroud)
还有一件事,似乎没有人提到过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>'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
134738 次 |
| 最近记录: |