Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel error 429 (Too Many Requests) : How to check and log requests count?

Tags:

laravel

I have the following error message in my console:

Failed to load resource: the server responded with a status of 429 (Too Many Requests)

So, my question is, from where can I get the number of requests to check and log when I'm close to the limit ?

like image 259
DevonDahon Avatar asked Oct 18 '25 07:10

DevonDahon


1 Answers

In client side, you can check the response headers

x-ratelimit-limit: 60
x-ratelimit-remaining: 59

enter image description here

if you send requests by axios

axios.get(`/api/users`)
    .then(({ data, headers }) => {
        console.log(headers['x-ratelimit-limit']);
    });

In server side, you can add a middleware like this

php artisan make:middleware LogRequestLimit

register it in app\Http\Kernel.php

    protected $middleware = [
        ...
        \App\Http\Middleware\LogRequestLimit::class,
    ];

app\Http\Middleware\LogRequestLimit.php

<?php
namespace App\Http\Middleware;
use Closure;

class LogRequestLimit
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        \Log::debug($response->headers->get('x-ratelimit-limit'));

        return $response;
    }
}

enter image description here

like image 196
DengSihan Avatar answered Oct 21 '25 06:10

DengSihan