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

pn523's avatar
Level 2

API not verifying token in Laravel 5.5

When sending an ajax request to api it does not verify the token, disabling verifyCsrfToken middleware in Ap\Http\kernel of api works. I strictly followed all the rules according to the docs in laravel 5.5, It works perfectly when redirecting internally in main project but breaks when sending post request to API. Any suggestions ? Thanks in advance.

0 likes
15 replies
pn523's avatar
Level 2

I think it has to do something with cross origin domain request, is that right ?

Geddit's avatar

My assumption is that you're making an API request to a route that uses the web middleware—as verifyCsrfToken is only part of that middleware group.

Make sure that your API requests are in routes/api.php.

If not, sharing some code would be nice!

pn523's avatar
Level 2

Actually I am not using auth and I did authorization manually, also my routes are in web.php

Geddit's avatar

This has nothing to do with authentication.

You might want to read the docs: Routing. It states:

"...These routes are assigned the web middleware group, which provides features like session state and CSRF protection. The routes in routes/api.php are stateless and are assigned the api middleware group."

1 like
pn523's avatar
Level 2

I tried by placing route in api.php but same error. I also tried response headers but it did not work.

If I exclude it, of course it works but it would mean that i need to exclude all the routes and I don't think it is a good idea. Any suggestions ? , Thanks in advance.

pn523's avatar
Level 2

I checked laravel log and all I figured out is that it throws 419 status because it is a TokenMismatchException, and after research I figured it out that I was on correct path and I need to allow Cross-Origin Resource Sharing(CORS) from API, I returned response with Headers as mentioned in DOCS but not working, any idea ?? I even tried laravel cors at "https://github.com/barryvdh/laravel-cors". Any Idea what is causing this ????

Snapey's avatar

you can only use csrf protection if you use sessions.

Do you use sessions in your own version of auth?

pn523's avatar
Level 2

Yes I am setting session on success function of post request to api via ajax.

Snapey's avatar

so your ajax call is passing the session cookie with each request?

pn523's avatar
Level 2

I solved the problem by placing "header('Access-Control-Allow-Origin : *')" in bootstrap/app.php, but that is little bit odd thing. Is it valid if on production ?

pn523's avatar
pn523
OP
Best Answer
Level 2

FINALLY SOLVED IT : In "vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php" under "isReading" method replace

return in_array($request->method(), ['HEAD', 'GET', 'OPTIONS']); To This

return in_array($request->method(), ['POST', 'HEAD', 'GET', 'OPTIONS']);

Cronix's avatar

That change can get overridden when you update laravel. You should never alter code in /vendor.

It would be better to create your own middleware and use that instead of the default. You can set which middleware runs in /app/Http/kernel.php. Just replace \App\Http\Middleware\VerifyCsrfToken::class, with your own class in the web middleware group.

1 like
pn523's avatar
Level 2

Ok, I will try it Thank you.

click's avatar

@pjn I can't believe this is a solution, this is just a workaround. What you just did is disabling csrf checking for post requests...

The method you are changing is isReading(). This method will return true after your change on post requests and will never fire the the TokenMismatchException anymore.

public function handle($request, Closure $next)
{
    if (
        $this->isReading($request) ||
        $this->runningUnitTests() ||
        $this->inExceptArray($request) ||
        $this->tokensMatch($request)
    ) {
        return $this->addCookieToResponse($request, $next($request));
    }

    throw new TokenMismatchException;
}

So for everyone else that is here for a solution: Do NOT change the isReading() method but actually solve the problem you have

2 likes

Please or to participate in this conversation.