Flask:如何在 Response 中设置状态码
Flask: How do I set the status code in Response
一切正常,API(flask) returns 数据很好。但是,当我尝试自定义响应代码时,我做不到。
以下是我试过的两种方式:
from flask import make_response
dictionary = {1:'a', 2:'b'}
resp = make_response(dictionary,1001)
return resp
#In developer tools, I could see the data, but status-code is 200
.
from flask import Response
dictionary = {1:'a', 2:'b'}
resp = Response(dictionary, status = 1001)
return resp
#Here again, I see the data, but the status code is 200
如何将状态代码设置为其他内容?
这应该可以回答您的问题:https://flask.palletsprojects.com/en/1.1.x/quickstart/#about-responses
来自文档的示例:
@app.errorhandler(404)
def not_found(error):
resp = make_response(render_template('error.html'), 404)
resp.headers['X-Something'] = 'A value'
return resp
一切正常,API(flask) returns 数据很好。但是,当我尝试自定义响应代码时,我做不到。
以下是我试过的两种方式:
from flask import make_response
dictionary = {1:'a', 2:'b'}
resp = make_response(dictionary,1001)
return resp
#In developer tools, I could see the data, but status-code is 200
.
from flask import Response
dictionary = {1:'a', 2:'b'}
resp = Response(dictionary, status = 1001)
return resp
#Here again, I see the data, but the status code is 200
如何将状态代码设置为其他内容?
这应该可以回答您的问题:https://flask.palletsprojects.com/en/1.1.x/quickstart/#about-responses
来自文档的示例:
@app.errorhandler(404)
def not_found(error):
resp = make_response(render_template('error.html'), 404)
resp.headers['X-Something'] = 'A value'
return resp