I have a simple REST API set up in my flask project, which I am trying to rate limit using the flask_limiter library.
from flask import Flask, request, redirect, session, render_template
from flask_restful import Api, Resource
from flask_limiter.util import get_remote_address
app = Flask(__name__)
api = Api(app)
# Api endpoint
class getData(Resource):
def get(self):
return {'data': 'data'}
api.add_resource(getData, "/data")
I tried adding the default @limiter.limit()
decorator, but it doesn't seem to work. When I tested it, only the default limits were working.
limiter = Limiter(
app,
key_func=get_remote_address,
default_limits=["1000 per day", "3 per hour"]
)
class getData(Resource):
@limiter.limit("1 per minute")
def get(self):
return {'data': 'data'}
Instead of limiting the access to 1 per minute, as specified in the decorator, it used default limiter values instead.
Is it possible to rate limit my API using this method and how can I do that?
I'm not sure if you solved your problem already but here is what will work in your situation. Need to include it in the method_decorators field that flask restful provides. More information can be found in the following links.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.