PythonFlask参数验证

2024-04-26 13:50:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我是flask的新手,面临一些问题,我需要根据输入请求在post函数中返回两件事。”200':说明:接受操作,“400”:说明:缺少必需的主体参数或类型不正确。 下面是我的代码

def post(self):
    parser = reqparse.RequestParser(bundle_errors=True)

    parser.add_argument('identifier', required=True)
    parser.add_argument('fine', required=True)

    # Parse the arguments into an object
    args = parser.parse_args()
    if args['identifier'] is None:
        return {'message': 'Mandatory body parameter missing or have incorrect type', 'data': args}, 400

    return {'message': 'operation accepted', 'data': args}, 200.

但当传入请求中没有标识符时,我无法返回400。我的语法有问题吗


Tags: 函数addtrueparserflaskmessagedatareturn
1条回答
网友
1楼 · 发布于 2024-04-26 13:50:44

在挖掘了一点之后,我发现应该删除required=True。如果我想得到我的回报值。否则,如果传入请求中缺少属性,Reqparser将引发错误

相关问题 更多 >