Reputation: 14768
Is there a way to disable rate limiting on every/individual routes in Laravel?
I'm trying to test an endpoint that receives a lot of requests, but randomly Laravel will start responding with { status: 429, responseText: 'Too Many Attempts.' }
for a few hundred requests which makes testing a huge pain.
Upvotes: 141
Views: 209222
Reputation: 51
Go to your RouteServiceProvider (app/Providers/RouteServiceProvider.php)
and then increse the Rate to remove it fro this
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60);
});
to this for example
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(600);
});
Upvotes: 2
Reputation: 1945
If you are using Laravel 8.x or later you can use RateLimiter with the following steps:
In your app/Providers/RouteServiceProvider.php find below configureRateLimiting:
protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
});
// no limit throttle
RateLimiter::for('none', function (Request $request) {
return Limit::none();
});
}
In your app/web.php add 'throttle:none:
Route::group(['middleware' => ['auth', 'throttle:none']], function ($router) {
Route::post('test', 'TestController@test');
});
Upvotes: 19
Reputation: 9
You can remove or comment out the line (throttle:60,1)
from App\Http\Kernel.php.
Upvotes: -3
Reputation: 109
In my case, I just changed the default value of perMinute()
in `App\Providers\RouteServiceProvider.
protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
$perMinute = env('APP_ENV') === 'testing' ? 1000 : 60;
return Limit::perMinute($perMinute)
->by(optional($request->user())->id ?: $request->ip());
});
}
Upvotes: 6
Reputation: 4735
You can use cache:clear
command to clear your cache including your rate limits, like so:
php artisan cache:clear
Upvotes: 30
Reputation: 2932
You can add the following lines in your app/Http/Kernel.php
'api' => [
'throttle:120,1',
'bindings',
\App\Library\Cobalt\Http\Middleware\LogMiddleware::class,
],
If the problem persists try the artisan command php artisan cache:clear
Upvotes: 5
Reputation: 49
A non-hacky way to increase the throttle in unit tests to avoid the dreaded 429:
$requestsPerMinute = ENV("REQUESTS_PER_MINUTE", 60);
Route::middleware(["auth:api", "throttle:$requestsPerMinute,1"])->group(function(){
// your routes
});
<server name="REQUESTS_PER_MINUTE" value="500"/>
Upvotes: 3
Reputation: 973
In Laravel 5.7
Dynamic Rate Limiting You may specify a dynamic request maximum based on an attribute of the authenticated User model. For example, if your User model contains a rate_limit attribute, you may pass the name of the attribute to the throttle middleware so that it is used to calculate the maximum request count:
Route::middleware('auth:api', 'throttle:rate_limit,1')->group(function () {
Route::get('/user', function () {
//
});
});
https://laravel.com/docs/5.7/routing#rate-limiting
Upvotes: 10
Reputation: 2644
You can actually disable only a certain middleware in tests.
use Illuminate\Routing\Middleware\ThrottleRequests;
class YourTest extends TestCase
{
protected function setUp()
{
parent::setUp();
$this->withoutMiddleware(
ThrottleRequests::class
);
}
...
}
Upvotes: 86
Reputation: 10254
If you want to disable just for automated tests, you can use the WithoutMiddleware
trait on your tests.
use Illuminate\Foundation\Testing\WithoutMiddleware;
class YourTest extends TestCase {
use WithoutMiddleware;
...
Otherwise, just remove the 'throttle:60,1',
line from your Kernel file (app/Http/Kernel.php
), and your problem will be solved.
Upvotes: 7
Reputation: 432
Assuming you are using the API routes then you can change the throttle in app/Http/Kernel.php or take it off entirely. If you need to throttle for the other routes you can register the middleware for them separately.
(example below: throttle - 60 attempts then locked out for 1 minute)
'api' => [
'throttle:60,1',
'bindings',
],
Upvotes: 26
Reputation: 13259
In app/Http/Kernel.php
Laravel has a default throttle limit for all api routes.
protected $middlewareGroups = [
...
'api' => [
'throttle:60,1',
],
];
Comment or increase it.
Upvotes: 217