【发布时间】:2014-11-03 15:39:07
【问题描述】:
在 Flask-RESTful 中,我们添加如下 api 路由
api.add_resource(CuteKitty,'/api/kitty')
class CuteKitty(Resource):
def get(self): return {}
def post(self): return {}
def put(self): return {}
def delete(self): return None, 204
这样GET /api/kitty --> 到CuteKitty.get() 方法;对所有 HTTP 动词都这样
假设我需要为我的 api 消费者提供一个可爱的 api,比如
POST /api/kitty/drink/milk ---> CuteKitty.drink(what="milk")
POST /api/kitty/meow ---> CuteKitty.meow()
如何使用api.add_resource实现上述路由
class CuteKitty(Resource):
def get(self): return {}
def post(self): return {}
def put(self): return {}
def delete(self): return None, 204
def drink(self,what="milk"): return {}
def meow(self): return {}
同样如何添加 /api/kitty/<int:kitty_id>/habits 这样的路由 --> CuteKitty.habits(kitty_id)
【问题讨论】:
-
破解示例! :)
标签: python flask flask-restful