简体   繁体   English

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

[英]Custom error messages with reqparse of flask-restplus (V 0.12.1)

I have been following the flask-restplus documentation . 我一直在遵循flask-restplus 文档

How to return only the message specified in the "help" parameter of the add_argument function. 如何仅返回在add_argument函数的“ help”参数中指定的消息。

When the error is raised, the string in the "help" parameter is spitted out but with another appended string. 出现错误时,“帮助”参数中的字符串随即吐出,但附加了另一个字符串。

Below is the code 下面是代码

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"

You can use flask-restplus to parse fields, he help you with validation. 您可以使用flask-restplus来解析字段,他可以帮助您进行验证。 Some links: 一些链接:

In your code, parser = reqparse.RequestParser() , change to parser = reqparse.RequestParser(bundle_errors=True) . 在您的代码中, parser = reqparse.RequestParser() ,更改为parser = reqparse.RequestParser(bundle_errors=True)

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

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