Laravel cURL POST抛出TokenMismatchException

时间:2015-03-22 09:30:29

标签: php rest curl laravel-5

我的POST应用程序的cURL请求有问题。 目前,我正在使用laravel 5构建RESTFUL注册功能。

此示例的路由是

localhost:8000/user/create

我使用终端

上的cURL函数传递值
curl -d 'fname=randy&lname=tan&id_location=1&email=randy@randytan.me&password=randytan&remember_token=Y&created_at=2015-03-03' localhost:8000/auth/register/

这是我的routes.php

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

这是我存储注册用户的功能

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);

    }

执行上述cURL语法,我收到此错误TokenMismatchException

您有什么想法吗?

因为我只在我的几个网址中实现中间件,而且这个cURL注册网址并没有严格执行任何身份验证机制。

先谢谢。

3 个答案:

答案 0 :(得分:10)

在Laravel 5(最新版本)中,您可以在 /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.
    ];


}

希望这有帮助。

答案 1 :(得分:2)

Laravel 5默认在中间件中强制执行CSFR令牌身份验证。

  1. 您可以在所选路线上停用CSFR以下是link
  2. 或者您可以尝试其中一些solutions。希望如此有用。

  3. 更改您的csfr令牌方法/app/Http/Middleware/VerifyCsrfToken.php

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

答案 2 :(得分:0)

在我的情况下,我需要在 api.php 上添加路线,而不是 web.php