自定义错误消息,要求重新获得flask-restplus(V 0.12.1)

时间:2018-12-29 14:04:24

标签: python flask flask-restplus

我一直在追踪烧瓶剩余的documentation

如何仅返回在add_argument函数的“ help”参数中指定的消息。

出现错误时,“帮助”参数中的字符串随即吐出,但附加了另一个字符串。

下面是代码

from flask_restplus import Resource, reqparse

class Auth(Resource):
""" User signup and login """

    def post(self):
        """ signup """  
        parser = reqparse.RequestParser()
        parser.add_argument('username', type=str, required=True, 
         help="Username is required")   
        args = parser.parse_args() 
        return {"return": "sign up page"}


  ## This is the expected result
{
    "errors": {
        "username": "Username is required"
    },
    "message": "Input payload validation failed"
}

## Received result
{
    "errors": {
        "username": "Username is required Missing required 
parameter in the JSON body or the post body or the query string"
    },
    "message": "Input payload validation failed"
}

#### I want to return the error message under "username" with only 
the message specified the "help"

1 个答案:

答案 0 :(得分:0)

您可以使用flask-restplus来解析字段,他可以帮助您进行验证。一些链接:

在您的代码parser = reqparse.RequestParser()中,更改为parser = reqparse.RequestParser(bundle_errors=True)