Flask-Restful
来源:互联网 发布:培养java工程师 编辑:程序博客网 时间:2024/05/01 05:51
Flask-Restful
1.安装
pip install Flask-Restful
2.注册组件
from flask_restful import Apirest_api=Api()rest_api.init_app(app)
3.GET/POST/PUT/DELETE请求
from flask_restful import Resourceclass PostApi(Resource): #get请求 def get(self): pass #post请求 def post(self): pass #put请求 def put(self): pass #delete请求 def delete(self): pass
4.注册路由
rest_api.add_resource(PostApi, '/api/post', '/api/post/<int:post_id>', endpoint='api' )
5.格式化输出
from flask_retful import fields,marshal_with#构建格式post_fields={ 'title':fields.String(), 'text':fields.String(), 'publish_date':fields.DateTime()}#注册格式class PostApi(Resource): @marshal_with(post_fields) def get(self,post_id=None): if post_id: print(post_id) post=Post.objects(id=post_id).get_or_404() return post else: args=post_get_parser.parse_args() page=args['page'] or 1 posts=Post.objects.paginate(page=page,per_page=10).items return posts
6.请求参数获取与限制
from flask_retful import reqparsepost_get_parser=reqparse.RequestParser()#参数名,类型限制,获取参数的位置,是否可选,错误提示post_get_parser.add_argument( 'page', type=int, location=['args','headers'], required=False, help='page out of index')class PostApi(Resource): @marshal_with(post_fields) def get(self,post_id=None): if post_id: print(post_id) post=Post.objects(id=post_id).get_or_404() return post else: #获取参数 args=post_get_parser.parse_args() page=args['page'] or 1 posts=Post.objects.paginate(page=page,per_page=10).items return posts
阅读全文
0 0
- Flask-Restful
- Python Flask设计Restful
- Flask-restful API演示
- 【Python】Flask-RESTful使用
- Flask--Restful(一)
- Flask--Restful(二)
- 使用 Flask-RESTful 设计 RESTful API
- flask-restful接收post传参
- 关于在Flask中使用Restful
- 使用flask 构建基本的 restful api
- flask restful 实现返回结果为 html
- 使用 Flask 设计 RESTful 的认证
- 使用 Flask 设计 RESTful 的认证
- Python使用Flask搭建RESTful API
- 使用Flask搭建Restful API后端服务器
- Python Flask Restful API 基础篇
- Flask提供的RESTful Web服务
- Python flask-restful的安装使用
- Batch vs Stochastic Gradient
- C#--结构、类与属性
- virtualenv,virtualenvwrapper,pyenv 三大虚拟环境使用
- jdk8 lambda 常用流操作
- 百度云离线VPS下载器
- Flask-Restful
- build opencv3.3.0 with VTK8.0, CUDA9.0 on ubuntu9.0
- 【zookeeper】简单的客户端命令
- 利用border写一个小三角形
- 抗日战争与元数学的兴起
- 《DOS命令一日通》附录
- matlab中自带的分类器
- CodeFroces Round 877 B
- Cookie和Session