简体   繁体   English

每个HTTP方法的Flask-RESTful自定义路由

[英]Flask-RESTful custom routes for each HTTP method

I have simple Resource class which defines some API methods: 我有简单的Resource类,它定义了一些API方法:

class RoomAPI(Resource):
    def get(self):
        # some code

    def post(self):
        # some code

    def put(self):
        # some code

Then I define my routes like this: 然后我定义这样的路线:

api.add_resource(RoomAPI,'/api/rooms/')

So, my question is: how can I make different routes for each HTTP methos using only one Resource class? 因此,我的问题是:如何仅使用一个Resource类为每个HTTP方法创建不同的路由?

I want to get such API: 我想获得这样的API:

GET /api/rooms/get/
POST /api/rooms/create/
PUT /api/rooms/update/

The short answer is, you shouldn't. 简短的答案是,您不应该这样做。 That's not RESTful at all. 那根本不是RESTful的。

However, if you really want to, I think you could do it like so: 但是,如果您确实愿意,我认为您可以这样做:

api.add_resource(RoomAPI,'/api/rooms/get', methods=['GET'])
api.add_resource(RoomAPI,'/api/rooms/create', methods=['PUT'])
api.add_resource(RoomAPI,'/api/rooms/update', methods=['POST'])

Since the unused **kwargs from add_resource get passed to add_url_rule() . 由于来自add_resource的未使用**kwargs传递给add_url_rule()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM