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




边栏推荐
猜你喜欢

Redis spike case, learn from Shang Silicon Valley teacher in station B

基于OpenCV的轮廓检测(2)

Introduction to database - a brief introduction to MySQL

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

数据库概论 - 数据库的介绍

MySQL的数据库有关操作

MySQL underlying data structure

延时队列的几种实现姿势?日常必备技能!

【1206. 设计跳表】

Spark Learning Notes (V) -- spark core core programming RDD conversion operator
随机推荐
How to design the red table of database to optimize the performance
数据库概论 - MySQL的简单介绍
注解@Autowired和@Resource的区别总结
768. Block II greed that can complete sorting at most
mysql底层数据结构
Explain tool actual operation
Spark: ranking statistics of regional advertising hits (small case)
flask_restful中reqparse解析器继承
What are "full five unique" and "full two unique"? Any difference?
DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库
Common weak password Encyclopedia
volatile关键字及其作用
Method of converting curtain article OPML into markdown
app端接口用例设计方法和测试方法
常见弱口令大全
JMeter distributed pressure measurement
榕树贷款C语言结构体里的成员数组和指针
Practice of online problem feedback module (XV): realize the function of online updating feedback status
How to interact with the server when the client sends an SQL message
Yilingsi T35 FPGA drives LVDS display screen