Markus Proctor
Markus Proctor

Reputation: 435

TokenMismatchException in VerifyCsrfToken.php line 67 using Route::post()

I understand that this is a common issue with Laravel 5.x, but my particular run-in with the problem is not through submitting a form. Instead, I am using postman to send data to a URL endpoint to test if data is successfully received. I'm working with Laravel 5.2 and I'm very much new to it!

Here is my routes.php file (related content)

Route::group(['middleware' => 'web'], function () {
    Route::post('/cart', 'CartController@buildcart');
});

Here is my CartController.php (entire file)

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class CartController extends Controller
{
    public function buildcart(){
        echo 'hello';
    }
}

As simple as that is, when I use postman to send random data to the /cart URL, I get

TokenMismatchException in VerifyCsrfToken.php line 67:

Can anyone help me understand why this is failing? I don't see how using

{{ csrf_token() }}

is the solution for this case since the data is coming from an external source.

Running list of things I've tried

Upvotes: 0

Views: 825

Answers (2)

Achraf Khouadja
Achraf Khouadja

Reputation: 6279

try add the route to this route group

  Route::group(array('before' => 'csrf', ['middleware' => 'web']), function () {

 Route::post('/cart', 'CartController@buildcart');

 });

EDIT : try to comment this line in app\kernel.php

   protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        //       \App\Http\Middleware\VerifyCsrfToken::class,
    ],

    'api' => [
        'throttle:60,1',
    ],
];

Upvotes: 1

linuxartisan
linuxartisan

Reputation: 2436

Try changing the middleware in the routes.php file. I think, you are getting the error as you are not authorized to use the route. Try this:

Route::group(['middleware' => 'guest'], function () {
    Route::post('/cart', 'CartController@buildcart');
});

Upvotes: 0

Related Questions