asnelzin
asnelzin

Reputation: 174

Flask-RESTful custom routes for each HTTP method

I have simple Resource class which defines some API methods:

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?

I want to get such API:

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

Upvotes: 2

Views: 1588

Answers (1)

Nathan Hoover
Nathan Hoover

Reputation: 636

The short answer is, you shouldn't. That's not RESTful at all.

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().

Upvotes: 5

Related Questions