Flask 仅为 GET 方法添加资源端点
Flask add resource endpoint for GET method only
如何让 Resource
仅在触发 GET
方法时将子域作为参数传递?
localhost:5009/my_url/9952
其中 get
方法应该根据 id(在本例中为 9952)做一些事情
和 localhost:5009/my_url
其中 post
不需要任何 id 但它接受 json 代替。
目前我有以下代码
app = Flask(name)
app.url_map.strict_slashes = False
api = Api(app)
api.add_resource(MyResourceClass, '/my_url/<int:item_id>', endpoint='item_id')
view.py
class MyResourceClass(Resource):
def get(self, item_id=None):
if not item_id:
# item ID was not provided over get method
return 404
# Do stuff
return 200
def post(self):
## Should accept JSON
## Does some stuff based on request
def delete(self):
## Deletes the item information
return 200
显然它需要 item_id
出现在 URL 否则 post 请求 returns 404
错误。我如何使 item_id
仅对 GET
方法是必需的?
我想通了
app = Flask(name)
app.url_map.strict_slashes = False
api = Api(app)
api.add_resource(MyResourceClass, '/my_url/', '/my_url/<int:item_id>', endpoint='item_id')
现在 GET
和 POST
将触发,无论 URL 中的 item_id
定义如何,必须调整 view.py
中的所有方法来处理这种情况其中 item_id
未提供
如何让 Resource
仅在触发 GET
方法时将子域作为参数传递?
localhost:5009/my_url/9952
其中 get
方法应该根据 id(在本例中为 9952)做一些事情
和 localhost:5009/my_url
其中 post
不需要任何 id 但它接受 json 代替。
目前我有以下代码
app = Flask(name)
app.url_map.strict_slashes = False
api = Api(app)
api.add_resource(MyResourceClass, '/my_url/<int:item_id>', endpoint='item_id')
view.py
class MyResourceClass(Resource):
def get(self, item_id=None):
if not item_id:
# item ID was not provided over get method
return 404
# Do stuff
return 200
def post(self):
## Should accept JSON
## Does some stuff based on request
def delete(self):
## Deletes the item information
return 200
显然它需要 item_id
出现在 URL 否则 post 请求 returns 404
错误。我如何使 item_id
仅对 GET
方法是必需的?
我想通了
app = Flask(name)
app.url_map.strict_slashes = False
api = Api(app)
api.add_resource(MyResourceClass, '/my_url/', '/my_url/<int:item_id>', endpoint='item_id')
现在 GET
和 POST
将触发,无论 URL 中的 item_id
定义如何,必须调整 view.py
中的所有方法来处理这种情况其中 item_id
未提供