Is there a way to apply rate limiting to a route, but only on successful responses. For example, if a user sends requests to the send/code
endpoint 5 times, if all are successful, the user is blocked from sending requests again. However, if 2 of them fail (e.g. validation errors or other issues), but 3 succeed, the user should try 2 more times within the given time.
I know to do a rate limit check before executing the request and then block or let the user continue. But is there a way to apply my logic or should I try a different approach?
This is the source code
}
Suppose my URL is Example.com/test_Rate_Limit_only_success.
In this example, when the user sends a request to the system, the application still validates the request (if an error occurs, the user will send an unlimited request). With the data valid, the speed limiting part will start working.
You may need to make your own middleware, but you can extend the
ThrottleRequests
class and customize how you want to handle the response:Then add your middleware to
Kernel.php
:Then use it in routing like the original throttle middleware:
NOTE: If you want to return a custom response built from
RateLimiter::for
you may have to overridehandleRequestUsingNamedLimiter
, I haven't done anything for that here.