Flask 学习-80.Flask-RESTX使用reqparse 解析器trim=True去掉字符两边空格

前言

reqparse.RequestParser() 解析器可以帮助我们经验请求参数。trim=True可以去掉字符两边空格

trim=True 使用

from flask_restx import Namespace, Resource, reqparse
api = Namespace('api', description='项目')


parser = reqparse.RequestParser(trim=True)
parser.add_argument('name', location='json', type=str)
parser.add_argument('address', location='json', type=str)


@api.route('/demo')
class ProjectDemoView(Resource):

    def post(self):
        """新增项目"""
        print('POST 请求参数', parser.parse_args())
        return {"msg": "ok"}

请求参数

{"name": " x x   "}

获取到的args 参数

 {'name': 'x x',  'address': None}

字符串左右两边的空格去掉了

posted @ 2022-09-28 10:51  上海-悠悠  阅读(208)  评论(0编辑  收藏  举报