Flask 学习-74.Flask-RESTX 查询参数args 校验
前言
通过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 参数。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2021-09-23 python测试开发django-139.Bootstrap 中关于图片的显示(img-responsive)
2020-09-23 python笔记47-面试题:如何判断字典a在字典b
2019-09-23 httprunner学习9-完整的用例结构(yaml&json)
2017-09-23 Appium+python自动化20-查看iOS上app元素属性