Consider that I have two Resource
classes:
class UserView(Resource):
def get(self, id):
...
def post(self):
...
def put(self, id):
...
def delete(self, id):
...
class AnotherUserView(Resource):
def get(self):
...
def put(self):
...
def delete(self):
...
api.add_resource(UserView, '/user/<int:id>')
api.add_resource(AnotherUserView, '/user')
Given the code above, the GET
, PUT
and DELETE
methods of UserView
all require a path parameter id
. Therefore, UserView
is mapped to the route /user/<int:id>
. However, the POST
method does not need the path parameter id
but is included under the route that provides the param id
, which will be confusing.
So now, I'm thinking if there is a way to specify which methods are allowed in a specific route (or endpoint), like what we can do with Flask
: @app.route('/user/<int:id>', methods=['GET', 'PUT', 'DELETE']
.
Expected what-I-can-do:
api.add_resource(UserView, '/user/<int:id>', methods=['GET', 'PUT', 'DELETE'])
api.add_resource(UserView, '/user', methods=['POST'])
api.add_resource(AnotherUserView, '/user', methods=['GET', 'PUT', 'DELETE'])
But this is actually not going to work, as the compiler tells me I'm overwriting view function UserView
.
I read the doc of Flask-RESTful
and found api.add_resource
doesn't have an arg of methods
that can be used to specify allowed HTTP methods as app.route
does. Is there a way to achieve this?