Search code examples
phplaravelguzzlegoutte

Using Goutte / Guzzle with Laravel to Submit Form with CSRF


I have two websites (both mine) and I am testing Guzzle.

I am trying to submit a search form. This search form has the standard Laravel CSRF token hidden field automatically generated "_token".

When submitting the field with goutte it gets an error. Checking my logs on the website I can see it is the Laravel "TokenMismatchException"

Do I need to do something special in goutte to make sure it is posting the auto generated "_token" hidden field?


Solution

  • You need to disable CSRF protection for that route.

    In app/Http/Middleware/VerifyCsrfToken.php add this code to beginning of handle() method:

    $openRoutes = ['free/route', 'free/too'];
    
    foreach($openRoutes as $route) {
        if ($request->is($route)) {
            return $next($request);
        }
    }