0

I'm trying to specify an optional parameter to my flask restful api.

I have the following route defined where mode is optional.

My route is defined like so

api.add_resource(TestController, "/test/<id>/<mode>")

My controller is defined like so:


class TestController(Resource):
    def get(
        self,
        id: int,
        mode: int = -1,
    ):

I read one way to do it is like so

api.add_resource(TestController, "/test/<id>/")
api.add_resource(TestController, "/test/<id>/<mode>")

But this causes an error for me and gives me : AssertionError: View function mapping is overwriting an existing endpoint function: loggers

user391986
  • 29,536
  • 39
  • 126
  • 205

1 Answers1

1

There are a few points I would change here.

  1. Remove the trailing / from the first declaration:

    api.add_resource(TestController, "/test/")

  2. Add expected types to the get parameters e.g.:

    api.add_resource(TestController, "/test/int:id") api.add_resource(TestController, "/test/int:id/int:mode")

  3. I wouldn't associate two different routes with the same class.

class TestController1(Resource): def get( self, id: int, ):

class TestController2(Resource): def get( self, id: int, mode: int = -1 ):

Then change your routes correspondingly.

If you must do so, then it's better to write the route like this:

api.add_resource(TestController, "/test/<int:id>", "/test/<int:id>/<int:mode>")

This stackoverflow post might be of use

dancingCamel
  • 206
  • 2
  • 8