简体   繁体   English

在 flask restx reqparser 中至少强制设置一个参数

[英]make at lease one argument mandatory in flask restx reqparser

from flask_restx import reqparse

parser = reqparse.RequestParser(bundle_errors=True)
parser.add_argument('foo', type=int)
parser.add_argument('bar', type=int)

I am implementing search endpoint where user can specify /item?foo=f or /item?bar=b to serach items.我正在实现搜索端点,用户可以在其中指定 /item?foo=f 或 /item?bar=b 来搜索项目。

I tried raising exception but this just breaks the entire app.我尝试引发异常,但这只会破坏整个应用程序。 What is the correct exception need to raise to display bundled error to end user?向最终用户显示捆绑错误需要引发的正确异常是什么?

args = parser.parse_args()
if not args.foo or not args.bar:
    raise Exception ("Specify Foo or Bar in query string to search item")

If I get you correctly, what you need is to raise a werkzeug HTTPException with the correct status code.如果我没听错,你需要的是使用正确的状态代码引发 werkzeug HTTPException。 Flask abort helper can help you with that Flask abort helper 可以帮助你

Something along this lines沿着这条线的东西

from flask import abort
abort(400, 'some error message')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM