randytan
randytan

Reputation: 1039

Laravel cURL POST Throwing TokenMismatchException

I have a problem with POST cURL request to my application. Currently, I'm building RESTFUL registration function using laravel 5.

The routes for this is example is

localhost:8000/user/create

I pass value using cURL function on terminal

curl -d 'fname=randy&lname=tan&id_location=1&[email protected]&password=randytan&remember_token=Y&created_at=2015-03-03' localhost:8000/auth/register/

And this is my routes.php

Route::post('user/create', 'UserController@create');

And this is my function to store the registration user

public function create()
    {
        //function to create user.
        $userAccounts = new User;
        $userAccounts->fname = Request::get('fname');
        $userAccounts->lname = Request::get('lname');
        $userAccounts->id_location = Request::get('id_location');
        $userAccounts->email = Request::get('email');
        $userAccounts->password = Hash::make(Request::get('password'));
        $userAccounts->created_at = Request::get('created_at');

        $userAccounts->save();

        return Response::json(array(
                'error' => false,
                'user'  => $userAccounts->fname . " " . $userAccounts->lname
            ), 200);

    }

Executing the cURL syntax above, I'm getting this error TokenMismatchException

Do you have any ideas?

Because I'm implementing middleware only in my few urls, and this cURL registration url is not tight into any authentication mechanism.

Thanks before.

Upvotes: 3

Views: 8094

Answers (3)

htafoya
htafoya

Reputation: 19273

In my case, i needed to add the route on api.php instead of web.php

Upvotes: 0

Mohit Aneja
Mohit Aneja

Reputation: 428

In Laravel 5 (latest version) you can specify routes you want to exclude in /app/Http/Middleware/VerifyCsrfToken.php

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        'rest-api/*', # all routes to rest-api will be excluded.
    ];


}

Hope this helps.

Upvotes: 10

Kamran
Kamran

Reputation: 2741

Laravel 5 enforces CSFR token authentication in middleware by default.

  1. you can disable CSFR on selected route Here is the link
  2. or you can try some of these solutions. Hope so it will help.

  3. changing your csfr token method /app/Http/Middleware/VerifyCsrfToken.php

    public function handle ($request, Closure $next) { if ( !$request->is("api/*")) { return parent::handle($request, $next); } return $next($request); }

Upvotes: 2

Related Questions