前言

通过api.model() 设置的模型,只能校验post请求的json格式,无法设置location参数校验get请求的查询参数

遇到的问题

当我访问一个分页查询地址​​/teacher?page=1&size=3​​ 需要校验page和size只能是数字类型, 于是首先想到的是如下方式

query_model = api.model('QueryModel', {
'page': fields.Integer(min=1, required=False, description='页数'),
'size': fields.Integer(min=1, required=False, description='每页最大数')
})


@api.route('/teacher')
class TeacherView(Resource):

@api.doc(description='查询')
@api.expect(query_model, validate=True)
def get(self):
"""查询全部"""
api.logger.info(f"GET query查询参数: {request.args}")
return {"msg": "succsss"}

会直接报400 BAD REQUEST,json解析错误:

{
"message": "Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)"
}

api.model() 是从传过来的json格式中解析数据,因为get请求没有传json格式的参数,直接就格式错误了。
Flask-RESTX 还有一种请求参数校验方式 reqparse.RequestParser()

location 位置

reqparse.RequestParser() 校验请求参数,可以设置location 位置

# Look only in the POST body
parser.add_argument('name', type=int, location='form')

# Look only in the querystring
parser.add_argument('PageSize', type=int, location='args')

# From the request headers
parser.add_argument('User-Agent', location='headers')

# From http cookies
parser.add_argument('session_id', location='cookies')

# From file uploads
parser.add_argument('picture', type=werkzeug.datastructures.FileStorage, location='files')

代码参考

# 解析get请求查询参数
page_parser = api.parser()
page_parser.add_argument(
'page', type=int, default=1, location='args', help='page must be int'
)
page_parser.add_argument(
'size', type=int, default=10, location='args', help='size must be int'
)


@api.route('/teacher')
class TeacherView(Resource):

@api.doc(description='查询')
@api.expect(page_parser, validate=True)
def get(self):
"""查询全部"""
args = page_parser.parse_args()
api.logger.info(f"GET query查询参数: {args}")
return {"msg": "succsss"}

或者使用flask_restx自带的reqparse.RequestParser()

from flask_restx import reqparse

page_parser = reqparse.RequestParser()
page_parser.add_argument(
'page', type=int, default=1, location='args', help='page must be int'
)
page_parser.add_argument(
'size', type=int, default=10, location='args', help='size must be int'
)

通过​​location='args'​​参数指定校验args 参数。