2017-04-05 50 views
1

我在 '飛行' 控制器此方法TokenMismatchException在VerifyCsrfToken.php這個地址:Laravel 5.2.31經由HttpRequester

http://localhost:8000/api/v1/flights 

有了這個內容:

{ 
"flightNumber":"JWM12345", 
"status":"ontime", 
"arrival": { 
    "datetime":"2016-04-10 22:34:01", 
    "iataCode":"A57" 
}, 
"departure": { 
    "datetime":"2016-04-10 21:34:01", 
    "iataCode":"9C1" 
}} 

我將內容類型設置爲 '應用/ JSON',然後我提交請求,並得到這個錯誤:

TokenMismatchException在VerifyCsrfToken.php線67:

in VerifyCsrfToken.php line 67 
at VerifyCsrfToken->handle(object(Request), object(Closure)) 
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49 
at ShareErrorsFromSession->handle(object(Request), object(Closure)) 
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64 
at StartSession->handle(object(Request), object(Closure)) 
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37 
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) 
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59 
at EncryptCookies->handle(object(Request), object(Closure)) 
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103 
at Pipeline->then(object(Closure)) in Router.php line 726 
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699 
at Router->dispatchToRoute(object(Request)) in Router.php line 675 
at Router->dispatch(object(Request)) in Kernel.php line 246 
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44 
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) 
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103 
at Pipeline->then(object(Closure)) in Kernel.php line 132 
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99 
at Kernel->handle(object(Request)) in index.php line 54 
at require_once('/home/dendi/Documents/GitProjects/airview/public/index.php') in server.php line 21 

我想什麼爲了讓我的請求成功?

+0

'_token'在POST要求,PUT,DELETE方法。如果您默認使用網絡中間件。 –

+0

在請求體內發送'_token'。 –

+0

HttpRequester,Header和Parameter中有選項。我試着用名字'_token'和值'{{csrf_token()}}',但我仍然有同樣的錯誤。 –

回答

0

_token在POST,PUT,DELETE方法中是必需的。如果您默認使用網絡中間件。

要獲取令牌,您可以創建一個使用csrf_token()函數獲取令牌並返回它的路由。 作出這條路線的請求獲得令牌,該令牌會像n2s68OPSzaMVYyiFvvDhlRwvFF55zDwKaQPjX8AS並把它在要求身體像

{ 
    "_token":"n2s68OPSzaMVYyiFvvDhlRwvFF55zDwKaQPjX8AS", 
    ... 
} 

,並提出要求。

OR

如果你不想使用CSRF vaildation對部分航線創造什麼都命名空間我用這個命名空間App\Http\Middleware

現在,一個新的VerifyCsrfToken類像這樣添加路由,你想不通過$except陣列中的csrf令牌進行驗證。

<?php 

namespace App\Http\Middleware; 

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier; 

class VerifyCsrfToken extends BaseVerifier { 

    protected $except = [ 
     'api/v1/flights' 
    ]; 

} 

現在kernel.php

protected $middleware = [ 
    'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode', 
    'Illuminate\Cookie\Middleware\EncryptCookies', 
    'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse', 
    'Illuminate\Session\Middleware\StartSession', 
    'Illuminate\View\Middleware\ShareErrorsFromSession', 
    'Illuminate\Foundation\Http\Middleware\VerifyCsrfToken', // <-- this 
] 

與此

protected $middleware = [ 
    'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode', 
    'Illuminate\Cookie\Middleware\EncryptCookies', 
    'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse', 
    'Illuminate\Session\Middleware\StartSession', 
    'Illuminate\View\Middleware\ShareErrorsFromSession', 
    'App\Http\Middleware\VerifyCsrfToken', // <-- this 
] 

希望這有助於替換此:)

+1

現在我使用第二種解決方案,它的作品。謝謝 –

0

我認爲你應該升級到Laravel 5.4,在5.4中有一個名爲api.php的路徑文件夾中的不同文件,在那裏你可以編寫你的api路由,它可以通過「api/ruote_name」訪問。

乾杯。

+0

謝謝,也許它會工作,如果升級到5.4。但是,在laravel更新之後,我遇到了一個問題,並且在5.2中仍然很安心。 –

+0

您可以嘗試一件事 - 您將請求主體中的數據作爲原始json格式發送,那麼我希望您的問題得到解決。通常情況下,如果Content-type是application/json,並且您在表單主體中發送數據,則會發生令牌不匹配。您可以將Content-Type設置爲application/json和body中的數據作爲原始json。 –

0

關於什麼是CSRF令牌,我認爲您應該閱讀this excellent answer。 Laravel使用名爲VerifyCsrfToken的中間件來驗證這些請求。

如果你想在Laravel 5.2禁用此,開放App/Http/Kernel.php

protected $middleware = [ 
     'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode', 
     'Illuminate\Cookie\Middleware\EncryptCookies', 
     'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse', 
     'Illuminate\Session\Middleware\StartSession', 
     'Illuminate\View\Middleware\ShareErrorsFromSession', 
     'Illuminate\Foundation\Http\Middleware\VerifyCsrfToken', 
] 

,取下'Illuminate\Foundation\Http\Middleware\VerifyCsrfToken'從中間件陣列。

0

真的很容易解決 - 只需添加這行:

{{ csrf_field() }} 

您的表單中。它將創建一個用於CSRF保護的隱藏字段,錯誤將消失。

+0

我使用名爲'HttpRequester'的API調試器,類似於Postman。如何以及在何處添加{{csrf_field()}}? –

+0

就在你的表單中,所以

{{csrf_field()}}