Is there anyway to apply rate limiting to the route but for only success responses. Like for example if user sends request to send/code
endpoint 5 times and if all of them was successful then block the user to send request again. But if 2 of them was unsuccessful (like validation error or something) but 3 was successful then user should have 2 more attempts for the given time.
I know rate limiting checks before request get executed, then block or let the user to continue. But is there anyway to apply my logic or should I try to approach differently?
2
Answers
You would probably need to make your own middleware, but you can extend the
ThrottleRequests
class and just customize how you want to handle responses:Then add your middleware to
Kernel.php
:Then use it in a route like the original throttle middleware:
Note: you may have to override
handleRequestUsingNamedLimiter
if you want to return a custom response built fromRateLimiter::for
, I have not done anything for that here.This is source code
}
Suppose my URL is Example.com/test_Rate_Limit_only_success.
In this example, when a user sends a request to the system, the application will still validate the request (and if there are errors, the user will send unlimited requests). In the case of valid data, the rate limiting part will start working.