2

I am developing a project in laravel8 (which is still new) and I want to hide some custom response headers(starting with x-) like:

X-RateLimit-Limit: 15
X-RateLimit-Remaining: 14

enter image description here

I want to customize this behavior and hide these headers, how can I easily find code (in a framework), which set's these headers

My code:


    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(15);
        });
    }

1 Answer 1

1

Let's say we can create a new App\Http\Middleware\ThrottleRequests class which will extend original \Illuminate\Routing\Middleware\ThrottleRequests. And override the method getHeaders to your own that will return empty array.

namespace App\Http\Middleware;

use Illuminate\Routing\Middleware\ThrottleRequests as OriginalThrottleRequests;

/**
...
 */
class ThrottleRequests extends OriginalThrottleRequests
{
    /**
     * @inheritdoc
     */
    protected function getHeaders($maxAttempts, $remainingAttempts, $retryAfter = null)
    {
        return [];
    }
}

And then all you need to do is to change middleware definitions inside app/Http/Kernel.php file. Property $routeMiddleware where you register 'throttle' middleware \Illuminate\Routing\Middleware\ThrottleRequests::class you can change to your own implementation \App\Http\Middleware\ThrottleRequests::class.

In my case it is:

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        //...
        'throttle' => \App\Http\Middleware\ThrottleRequests::class,
        //...
    ];

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.