Skip to content Skip to sidebar Skip to footer

Flask-limiter Does Not Work With Flask-restful Api-based Application

I am trying to build a RESTful API app with flask_restful, flask_jwt_extended for user authorization and flask_limiter to limit the quota of a user at 6/minute. My toy/testing code

Solution 1:

I had the similar issue and found a solution that seems to work. Flask-Restful does things a little differently. It creates pluggable views. There's a section in Flask-Limiter docs about this particular case - https://flask-limiter.readthedocs.io/en/stable/#using-flask-pluggable-views

classTest(Resource):

  decorators = [limiter.limit("5/minute")]

  defget(self):
     ...

Instead of providing the decorator via @limiter.limit, we need to provide it this way.

I also had an issue about where to get the reference of limiter from. In some base directory, you can implement this and import in your class.

from flask importFlaskfrom flask_limiter importLimiterfrom flask_limiter.utilimport get_remote_address

_app = Flask(__name__)
limiter = Limiter(
    _app,
    key_func=get_remote_address
)

Post a Comment for "Flask-limiter Does Not Work With Flask-restful Api-based Application"