1

I'm writing a flask application. It makes sense to have multiple endpoints, like that:

prefix + '/'
prefix + '/<id>'
prefix + '/<id>/parameters'
prefix + '/<id>/parameters/<param>'

However, if I try to declare them all inside a blueprint, I'm getting a AssertionError: Handler is overwriting existing for endpoint _blueprintname_._firsthandlername_

Is there any way around this? I know it's been straight-forwardly done before, in technologies like .net core. Thanks in advance.

ntakouris
  • 898
  • 1
  • 9
  • 22
  • i dont think thats how you do REST with flask, take a look [here](https://flask-restful.readthedocs.io/en/0.3.5/quickstart.html) – Nullman Mar 24 '19 at 10:46

1 Answers1

0

If you plan to add many parameters in your routes, you could have a look at this module for flask. It helps you assigning routes to resources.

You could build up a set of routes as follows:

from flask import Flask, request
from flask_restful import Resource, Api, reqparse

app = Flask(__name__)
api = Api(app)

class Some(Resource):
  def get(self, id=None, params=None):
    """
      In order to allow empty params for routes, the named arguments
      must be initialized 
    """
    if id and params:
      return {'message':'this is get with id and params'}
    if id:
      return {'message':'this is get with id'}

    return {'message':'this is get'}


  def post():
    """
      One can use here reqparse module to validate post params
    """
    return {'message':'this is post'}

# Add the resource to the service 
api.add_resource(Some, '/', '/<string:id>','/<string:id>/<string:params>', ...)

# start the service
if __name__ == '__main__':
  app.run(debug=True)
Evhz
  • 8,852
  • 9
  • 51
  • 69
  • 1
    This was the thing I wanted to avoid, but I guess an extra layer of multiplexing is needed. Thanks. – ntakouris Mar 24 '19 at 13:17
  • I agree with you. I avoid using external libs as much as possible. An exception could be this case in which I found it quite simpler. Just have a look at the werkzeug [docs](https://werkzeug.palletsprojects.com/en/0.15.x/request_data/?highlight=request) on how to handle data if you wish to dig in the request object itself. Glad it helped. – Evhz Mar 24 '19 at 14:29