flask - something more strict than @api.expect for input data?

后端 未结 3 1445
一向
一向 2021-02-04 17:18

In my flask-restplus API I\'d like not only to check that input data, like in the following example

resource_fields = api.model(\'Resource\', {
    \'name\': fie         


        
3条回答
  •  佛祖请我去吃肉
    2021-02-04 18:09

    Instead of using a dictionary for your fields, try using a RequestParser (flask-restplus accepts both as documented here. That way, you can call parser.parse_args(strict=True) which will throw a 400 Bad Request exception if any unknown fields are present in your input data.

    my_resource_parser = api.parser()
    my_resource_parser.add_argument('name', type=str, default='string: name', required=True)
    my_resource_parser.add_argument('state', type=str, default='string: state')
    
    @api.route('/my-resource/')
    class MyResource(Resource):
        def post(self):
            args = my_resource_parser.parse_args(strict=True)
            ...
    

    For more guidance on how to use the request_parser with your resource, check out the ToDo example app in the flask-restplus repo.

提交回复
热议问题