Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

LaraBABA's avatar

How to add middleware to API?

Hello,

I have added the TrimStrings class in my kernel but it has no effects in my API request.

    protected $middleware = [
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\TrustProxies::class,

    ];

I have just added a blank space in my login username and the blank space is not trimmed in the API side of Laravel.

Any idea why please?

I am on Laravel 5.6

Thank you.

0 likes
2 replies
manojo123's avatar
Level 14

Please try to add the TrimStrings like this

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\TrimStrings::class,
....
1 like

Please or to participate in this conversation.