一切正常,API(flask)很好地返回了数据。但是,当我尝试自定义响应代码时,我做不到。以下是我尝试过的两种方法:
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://www.flaskapi.org/api-guide/status-codes/
来自Flask API页面的示例:
from flask_api import status
...
@app.route('/empty-view/')
def empty_view(self):
content = {'please move along': 'nothing to see here'}
return content, status.HTTP_404_NOT_FOUND