烧瓶:如何在响应中设置状态代码

问题描述 投票:0回答:1

一切正常,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

如何将状态代码设置为其他内容?

python flask http-status-codes
1个回答
0
投票

这应该可以回答您的问题: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
© www.soinside.com 2019 - 2024. All rights reserved.