当前位置:网站首页>不同风格的Flask-restful
不同风格的Flask-restful
2022-08-09 07:57:00 【scwMason】
参考文章http://www.pythondoc.com/Flask-RESTful/quickstart.html
http://www.pythondoc.com/Flask-RESTful/reqparse.html
总结一下这个模块:
1.可以利用 add_resource 批量处理url,不用再在给每个函数之前加上@app.route
2.flask-restful最重要的功能就是对于post携带数据的请求的参数解析,大致可以理解为:
首先需要创建对象:
parser = reqparse.RequestParser()
parser.add_argument('task', type=str)
然后解析参数储存在args中
args = parser.parse_args()
参数位置
parser.add_argument('name', type=int, location='form')
# Look only in the querystring
parser.add_argument('PageSize', type=int, location='args')
# From the request headers
parser.add_argument('User-Agent', type=str, location='headers')
# From http cookies
parser.add_argument('session_id', type=str, location='cookies')
# From file uploads
parser.add_argument('picture', type=werkzeug.datastructures.FileStorage, location='files')
更换名称
如果你不想以参数名来获取这个参数值,那么可以:
parser.add_argument('name', type=str, dest='public_name')
args = parser.parse_args()
args['public_name']
必需的参数
要求一个值传递的参数,只需要添加 required=True
来调用 add_argument()
。
parser.add_argument('name', type=str, required=True,
help="Name cannot be blank!")
获取不到的参数
我们将"session_id"设置成cookie的话,我们的args是获取不到的:
parser.add_argument('session_id',type=str,location="cookies")
args=parser.parse_args()
发送请求:
curl http://localhost:5000/todos -d "task=soming new" -d "name=mason" -d "session_id=hello word" -X POST -v
打印args
多个值传递
parser.add_argument('session_id',type=str,location="cookies",action="append")
这时候对于session_id传递值的时候就需要: -d "session_id=123" -d "session_id=456"这样了
这些是我觉得文章中比较重要的点,自己总结归纳了一下
边栏推荐
猜你喜欢
The String class objects created by the JVM memory allocation and the difference between equals and = =
ImportError: cannot import name ‘imresize‘
Flexible and easy-to-use sql monitoring script part7
String类创建的对象在JVM中的内存分配和equals与==的区别
C language: detailed explanation of soda bottle
VRRP原理及配置
Exclude null values when Oracle limits
如何生成dll文件 采用VS2017生成dll文件(动态库文件)和lib文件(静态库文件)以C语言为例
环形链表问题(判环、求入口点)
Redis(八)集群
随机推荐
(五)、马尔科夫预测模型
One-click login server script
.net(四) 数据层实现
种子数据报错:liquibase.exception.ValidationFailedException: Validation Failed
ImportError: cannot import name ‘imresize‘
三层交换机原理及配置
传输层协议介绍
SSM integration development case
C language: adjust the order of odd and even numbers
Collection 接口 & List 接口
(error) NOAUTH Authentication required.
权限(上)
EXCEL uses function joint debugging (find, mid, vlookup, xlookup)
Anaconda replaces the default virtual environment
如何把无用的代码注释为 Deprecated 弃用
yolov5检测数据集标签数量
pragma comment的使用(转载)重新排版
【机器学习】降维代码练习
Set集合
Kotlin Coroutines - Exception Handling