当前位置:网站首页>flask_restful中reqparse解析器继承
flask_restful中reqparse解析器继承
2022-07-27 01:36:00 【IT之一小佬】
示例代码1:
from flask import Flask
from flask_restful import reqparse, Api, Resource
app = Flask(__name__)
api = Api(app)
USERS = {
'row1': {'name': 'aaa', 'rate': [70, 65]},
'row2': {'name': 'bbb', 'rate': [80, 90, 68]},
'row3': {'name': 'ccc', 'rate': [90, 80]},
}
parser = reqparse.RequestParser()
parser.add_argument('name', type=str, required=True) # required=True 必选
parser.add_argument('rate', type=int, help='rate is a number', action='append') # action='append' 可重复选的值
parser.add_argument('User-Agent', type=str, location='headers')
# # 解析器继承
parser_copy = parser.copy()
parser_copy.add_argument('bar', type=int)
parser_copy.replace_argument('bar', type=str, required=True)
parser_copy.remove_argument('User-Agent')
# 真正处理请求的地方
class UserInfo(Resource):
def get(self):
return USERS, 200
def post(self):
args = parser.parse_args()
# args = parser_copy.parse_args()
user_id = int(max(USERS.keys()).lstrip('row')) + 1
user_id = 'row%i' % user_id
USERS[user_id] = {'name': args['name'], 'rate': args['rate']}
USERS[user_id]['ua'] = args.get('User-Agent')
USERS[user_id]['bar'] = args.get('bar')
return USERS[user_id], 201
api.add_resource(UserInfo, '/')
if __name__ == '__main__':
app.run(debug=True)
运行结果:




示例代码2: 【注意看和示例代码1中的区别】
from flask import Flask
from flask_restful import reqparse, Api, Resource
app = Flask(__name__)
api = Api(app)
USERS = {
'row1': {'name': 'aaa', 'rate': [70, 65]},
'row2': {'name': 'bbb', 'rate': [80, 90, 68]},
'row3': {'name': 'ccc', 'rate': [90, 80]},
}
parser = reqparse.RequestParser()
parser.add_argument('name', type=str, required=True) # required=True 必选
parser.add_argument('rate', type=int, help='rate is a number', action='append') # action='append' 可重复选的值
parser.add_argument('User-Agent', type=str, location='headers')
# # 解析器继承
parser_copy = parser.copy()
parser_copy.add_argument('bar', type=int)
parser_copy.replace_argument('bar', type=str, required=True)
parser_copy.remove_argument('User-Agent')
# 真正处理请求的地方
class UserInfo(Resource):
def get(self):
return USERS, 200
def post(self):
# args = parser.parse_args()
args = parser_copy.parse_args()
user_id = int(max(USERS.keys()).lstrip('row')) + 1
user_id = 'row%i' % user_id
USERS[user_id] = {'name': args['name'], 'rate': args['rate']}
USERS[user_id]['ua'] = args.get('User-Agent')
USERS[user_id]['bar'] = args.get('bar')
return USERS[user_id], 201
api.add_resource(UserInfo, '/')
if __name__ == '__main__':
app.run(debug=True)
运行结果:




边栏推荐
- “date: write error: No space left on device”解决
- [flask] the server obtains the file requested by the client
- Data Lake (20): Flink is compatible with iceberg, which is currently insufficient, and iceberg is compared with Hudi
- 优炫数据库集群如何唯一标识一条用户SQL
- Sqlserver select * can you exclude a field
- 基于.NetCore开发博客项目 StarBlog - (16) 一些新功能 (监控/统计/配置/初始化)
- HCIP第十三天笔记
- Worthington果胶酶的特性及测定方案
- docker 创建mysql 8.x容器,支持mac ,arm架构芯片
- AcWing 2074. 倒计数 模拟
猜你喜欢

Baidu cloud face recognition

1.28亿美元!芬兰量子计算公司IQM获世界基金支持

vector 转 svg 方法

30 minutes to thoroughly understand the synchronized lock upgrade process

Portraiture5 new and upgraded leather filter plug-in artifact

Social wechat applet of fanzhihu forum community

185. 部门工资前三高的所有员工(必会)

快速排序及优化

spark:地区广告点击量排行统计(小案例)

图解用户登录验证流程,写得太好了!
随机推荐
OC-消息机制
【常用搜索问题】111
Sqlserver select * can you exclude a field
常见弱口令大全
Yilingsi T35 FPGA drives LVDS display screen
国内服务器与海外服务器用1个数据库,怎样可以访问的快?
spark学习笔记(五)——sparkcore核心编程-RDD转换算子
opiodr aborting process unknown ospid (21745) as a result of ORA-609
Deep learning vocabulary embedded, beam search
be based on. NETCORE development blog project starblog - (16) some new functions (monitoring / statistics / configuration / initialization)
shell awk
延时队列的几种实现姿势?日常必备技能!
【学习笔记之菜Dog学C】字符串+内存函数
The EXE compiled by QT is started with administrator privileges
Data Lake (20): Flink is compatible with iceberg, which is currently insufficient, and iceberg is compared with Hudi
AcWing 2074. 倒计数 模拟
图解用户登录验证流程,写得太好了!
数据湖(二十):Flink兼容Iceberg目前不足和Iceberg与Hudi对比
Jmeter分布式压测
数据库红的表如何设计才能使性能更加优化