简体   繁体   English

Laravel cURL POST抛出TokenMismatchException

[英]Laravel cURL POST Throwing TokenMismatchException

I have a problem with POST cURL request to my application. 我对我的应用程序的POST cURL请求有问题。 Currently, I'm building RESTFUL registration function using laravel 5. 目前,我正在使用laravel 5构建RESTFUL注册功能。

The routes for this is example is 这个例子的路线是

localhost:8000/user/create

I pass value using cURL function on terminal 我在终端上使用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/

And this is my routes.php 这是我的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 执行上面的cURL语法,我收到此错误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. 因为我只在我的几个网址中实现中间件,而且这个cURL注册网址并没有严格执行任何身份验证机制。

Thanks before. 谢谢你。

In Laravel 5 (latest version) you can specify routes you want to exclude in /app/Http/Middleware/VerifyCsrfToken.php 在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.
    ];


}

Hope this helps. 希望这可以帮助。

Laravel 5 enforces CSFR token authentication in middleware by default. Laravel 5默认在中间件中强制执行CSFR令牌认证。

  1. you can disable CSFR on selected route Here is the link 你可以在选定的路线上禁用CSFR这是链接
  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 更改您的csfr令牌方法/app/Http/Middleware/VerifyCsrfToken.php

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM