flask-restful使用总结
作者:stray 时间:2023-02-10 13:24:21
Flask-RESTful是一个用于快速创建RESTful API接口的Flask扩展。使用Flask-RESTful可以很快速方便地创建一个RESTful风格的接口应用程序。
1、安装
pip install flask_restful
2、全部代码
from flask import Flask
from flask_restful import reqparse, abort, Api, Resource
app = Flask(__name__)
api = Api(app)
TODOS = {
'todo1': {'task': 'build'},
'todo2': {'task': 'bui2'},
'todo3': {'task': 'bui3'}
}
parser = reqparse.RequestParser()
parser.add_argument('task')
class Todo(Resource):
def get(self, todo_id):
return TODOS[todo_id]
def delete(self, todo_id):
del TODOS[todo_id]
return '', 204
def put(self, todo_id):
args = parser.parse_args()
task = {'task': args['task']}
TODOS[todo_id] = task
return task, 201
class TodoList(Resource):
def get(self):
return TODOS
api.add_resource(TodoList, '/todos')
api.add_resource(Todo, '/todos/<todo_id>')
if __name__ == '__main__':
app.run(debug=True)
3、代码说明
1). 初始化
在当前程序初始化一个flask-restful的api实例:
api = Api(app)
2). 定义资源类
资源类是restful接口的具体实现,需要继承Resource类。在类中可以定义get、post等符合restful规范的接口。
class Todo(Resource):
def get(self, todo_id):
return TODOS[todo_id]
def delete(self, todo_id):
del TODOS[todo_id]
return '', 204
def put(self, todo_id):
args = parser.parse_args()
task = {'task': args['task']}
TODOS[todo_id] = task
return task, 201
3). 参数解析
虽然 Flask 提供了简单的方法访问请求数据, 但是对于表单数据处理还是比较麻烦. Flask-RESTful 提供了 reqparse 一种类似 argparse 参数解析机制以简化参数处理.
parser = reqparse.RequestParser(): reqparse使用RequestParser() 可以从 flask.Request.values,以及 flask.Request.json 解析值。这里创建了一个解析对象parser。
parser.add_argument('task'):向parser对象中添加你要关注的命令行参数和选项,每一个add_argument方法对应一个你要关注的参数或选项。
parser.parse_args():调用parse_args()方法进行解析,解析成功之后即可使用。
4). add_argument扩展
对于必传的参数,可以设置required=True
parser.add_argument('name', type=str, required=True,
help="Name cannot be blank!")
传多个值,使用 action='append'
parser.add_argument('name', type=str, action='append')
使用:
curl http://api.example.com -d "Name=bob" -d "Name=sue" -d "Name=joe"
获取的参数为:
args = parser.parse_args()
args['name'] # ['bob', 'sue', 'joe']
来源:https://segmentfault.com/a/1190000017250771
标签:flask,restful
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python面向对象基础之常用魔术方法
2021-08-20 20:08:59
![](https://img.aspxhome.com/file/2023/9/87639_0s.png)
使用 laravel sms 构建短信验证码发送校验功能
2024-05-03 15:28:42
想学画画?python满足你!
2022-03-28 02:52:30
![](https://img.aspxhome.com/file/2023/1/114601_0s.png)
Python os.mkdir()与os.makedirs()的使用区别
2023-08-19 20:01:56
基于MATLAB和Python实现MFCC特征参数提取
2023-01-29 07:37:53
![](https://img.aspxhome.com/file/2023/1/95731_0s.png)
如何用Navicat操作MySQL
2024-01-15 20:56:37
![](https://img.aspxhome.com/file/2023/5/77215_0s.png)
PHP生成随机数的方法实例分析
2024-05-03 15:49:09
Python解释器及PyCharm工具安装过程
2021-02-23 08:21:51
![](https://img.aspxhome.com/file/2023/8/118508_0s.jpg)
基于Token的身份验证的方法
2023-09-27 19:05:41
css可以给img元素设置背景图
2008-09-29 15:35:00
python使用pil生成缩略图的方法
2022-06-07 13:07:44
详解从vue的组件传值着手观察者模式
2024-05-10 14:19:01
![](https://img.aspxhome.com/file/2023/3/126413_0s.jpg)
XML卷之实战锦囊(2):动态查询
2008-09-05 17:20:00
python中使用.py配置文件的方法详解
2022-02-13 19:57:22
MySQL延时复制库方法详解
2024-01-26 14:21:51
Python可视化最频繁使用的10大工具总结
2022-07-01 04:00:17
Python3.5面向对象与继承图文实例详解
2021-05-14 15:48:42
![](https://img.aspxhome.com/file/2023/4/102944_0s.png)
Python OpenCV 使用滑动条来调整函数参数的方法
2022-05-20 06:43:48
![](https://img.aspxhome.com/file/2023/4/76144_0s.jpg)
大写锁定提醒
2009-06-11 13:12:00
![](https://img.aspxhome.com/file/UploadPic/20096/11/win2003_capslk-48s.png)
MySQL数据库优化详解
2024-01-23 12:51:55