Flask 学习-82.Flask-RESTX使用reqparse 解析器校验枚举类型choices 参数
前言
reqparse.RequestParser() 解析器可以校验枚举类型,在add_argument中使用choices参数
choices 设置参数可选值
比如性别设置可选项:男、女
def post(self):
# 校验入参
parser = reqparse.RequestParser()
parser.add_argument('username', required=True, type=str, nullable=False, help='username is required')
parser.add_argument('password', required=True, type=str, nullable=False, help='password is required')
parser.add_argument('sex', choices=["男", "女"], type=str, help='sex invalid')
args = parser.parse_args()
print(f'请求入参:{args}')
请求示例,sex不是可选项的时候会报400
POST http://127.0.0.1:5000/api/v1/register HTTP/1.1
User-Agent: Fiddler
Host: 127.0.0.1:5000
Content-Type: application/json
Content-Length: 73
{
"username": "test",
"password" : "111111",
"sex": "x"
}
HTTP/1.0 400 BAD REQUEST
Content-Type: application/json
Content-Length: 152
Server: Werkzeug/2.0.1 Python/3.8.5
Date: Sun, 04 Sep 2022 12:26:41 GMT
{
"errors": {
"sex": "sex invalid The value 'x' is not a valid choice for 'sex'."
},
"message": "Input payload validation failed"
}
【推荐】国内首个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-28 python测试开发django -144.Ace Editor 在线编辑python代码
2019-09-28 httprunner学习15-运行用例命令行参数详解
2019-09-28 httprunner学习14-完整的项目结构设计
2019-09-28 httprunner学习13-环境变量.env
2018-09-28 python笔记32-ddt框架优化(生成html报告注释内容传变量)
2018-09-28 python笔记31-使用ddt报告出现dict() -> new empty dictionary dict(mapping) 问题解决
2018-09-28 python笔记30-docstring注释添加变量