当前位置:网站首页>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 :




边栏推荐
- Deeply understand the underlying data structure and algorithm of MySQL index
- Explain详解
- 基于OpenCV的轮廓检测(2)
- Textbox in easyUI inserts content at the cursor position
- 在typora中插入图片和视频
- Volatile keyword and its function
- 如何进行 360 评估
- 百融榕树数据模型
- How to optimize MySQL
- It's too strong. An annotation handles the data desensitization returned by the interface
猜你喜欢

Explain

MySQL Chinese failure

Double disk: the main differences between DFS and BFS, the differences in ideology, and the differences in code implementation

太强了,一个注解搞定接口返回数据脱敏

Quick sequencing and optimization

30 minutes to thoroughly understand the synchronized lock upgrade process

Deeply understand the underlying data structure and algorithm of MySQL index

带你了解什么是 Web3.0

Introduction to redis

30分钟彻底弄懂 synchronized 锁升级过程
随机推荐
477-82(236、61、47、74、240、93)
Mysql: summary of common sub database and sub table schemes of Internet companies
Database usage security policy
768. 最多能完成排序的块 II 贪心
Insert pictures and videos in typera
Textbox in easyUI inserts content at the cursor position
【学习笔记之菜Dog学C】字符串+内存函数
Introduction to redis
Typescript TS basic knowledge interface, generics
Spark Learning Notes (V) -- spark core core programming RDD conversion operator
若依框架代码生成详解
一种分布式深度学习编程新范式:Global Tensor
Deeply understand the underlying data structure and algorithm of MySQL index
【1206. 设计跳表】
opiodr aborting process unknown ospid (21745) as a result of ORA-609
Network security / penetration testing tool awvs14.9 download / tutorial / installation tutorial
Learn the recycling mechanism of recyclerview again
[learning notes, dog learning C] string + memory function
MySQL Chinese failure
[regular] judgment, mobile number, ID number