当前位置:网站首页>flask_ Reqparse parser inheritance in restful
flask_ Reqparse parser inheritance in restful
2022-07-27 03:37:00 【One of IT guys】
Sample code 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 Mandatory
parser.add_argument('rate', type=int, help='rate is a number', action='append') # action='append' Recheckable value
parser.add_argument('User-Agent', type=str, location='headers')
# # The parser inherits
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')
# Where the request is actually handled
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)
Running results :




Sample code 2: 【 Pay attention to the sample code 1 The difference between 】
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 Mandatory
parser.add_argument('rate', type=int, help='rate is a number', action='append') # action='append' Recheckable value
parser.add_argument('User-Agent', type=str, location='headers')
# # The parser inherits
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')
# Where the request is actually handled
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)
Running results :




边栏推荐
- 索引最佳实践
- 数字孪生实际应用:智慧城市项目建设解决方案
- Activiti5.22.0 extension supports domestic databases, taking gbase database as an example
- J-3-point practice in the second game of 2022 Niuke multi school
- 百融榕树数据模型
- Yilingsi T35 FPGA drives LVDS display screen
- mysql出现不存在错误
- 【树链剖分】模板题
- 常见弱口令大全
- Duplicate disc: what are the basic attributes of an image? What do you know about images? What are the parameters of the image
猜你喜欢

Explain

477-82(236、61、47、74、240、93)

Details of impala implementation plan

Mysql: summary of common sub database and sub table schemes of Internet companies

Contour detection based on OpenCV (2)

基于OpenCV的轮廓检测(1)

30分钟彻底弄懂 synchronized 锁升级过程

Pytorch损失函数总结

【树链剖分】模板题

Redis spike case, learn from Shang Silicon Valley teacher in station B
随机推荐
阶乘末尾0的数量
easyui中textbox在光标位置插入内容
Csu18m91 is used as the master controller of the intelligent scale scheme
Spark: calculate the average value of the same key in different partitions (entry level - simple implementation)
【无标题】JDBC连接数据库读超时
[learn FPGA programming from scratch -54]: high level chapter - FPGA development based on IP core - principle and configuration of PLL PLL IP core (Altera)
数据库概论 - MySQL的简单介绍
Data analysis and disassembly method of banyan tree in Bairong
Introduction to database - a brief introduction to MySQL
Activiti5.22.0 extension supports domestic databases, taking gbase database as an example
将幕布文章OPML转换为Markdown的方法
30分钟彻底弄懂 synchronized 锁升级过程
MySQL:互联网公司常用分库分表方案汇总
DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库
技术风向标 | 云原生技术架构成熟度模型解读
How can you access the domestic server and overseas server quickly with one database?
volatile关键字及其作用
Redis源码学习(33),命令执行过程
Common weak password Encyclopedia
opiodr aborting process unknown ospid (21745) as a result of ORA-609