I use Dingo with Laravel 5.1 to create simple API.
So at route.php I have:
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function($api) {
$api->get('getvoucher', 'App\Http\Controllers\BitemsController@index');
$api->get('update/{key}', 'App\Http\Controllers\BitemsController@update');
$api->post('store', 'App\Http\Controllers\BitemsController@store');
$api->post('authenticate', 'App\Http\Controllers\AuthenticateController@authenticate');
$api->post('logout', 'App\Http\Controllers\AuthenticateController@logout');
$api->get('token', 'App\Http\Controllers\AuthenticateController@getToken');
});
and my BitemsController is:
public function index(Request $request)
{
$bitem = Bitem::where('key',$request->key)->where('id',$request->pin)->first();
return $bitem;
}
public function store(Request $request)
{
$bitem = new Bitem($request->all());
$bitem->save;
return $bitem;
}
Now I use POSTMAN application to test the API, and when I send GET to localhost:8888/api/getvoucher everything is fine, but when I make POST request to store some data then I got error:
"message": "500 Internal Server Error",
"status_code": 500,
"debug": {
"line": 53,
"file": "C:\\wamp\\www\\dine\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken.php",
"class": "Illuminate\\Session\\TokenMismatchException",
"trace": [
POSTMAN:
To fix the problem I try to add:
protected $except = [
'api/*',
];
inside middleware VerifyCsrfToken.php but wnt work.
Please tell me how to solve my problem...
When a POST request is made, the VerifyCSRFToken middleware handles the request. This middleware is a part of Laravel and its class extension is defined here: app/Http/Middleware/VerifyCsrfToken.php.
Additional middleware can be written to perform a variety of tasks besides authentication. For example, a logging middleware might log all incoming requests to your application. There are several middleware included in the Laravel framework, including middleware for authentication and CSRF protection.
GitHub - dingo/api: A RESTful API package for the Laravel and Lumen frameworks. Failed to load latest commit information. Unfortunately this package cannot be maintained at this location anymore due to broken CI integrations, and travis-ci likely can't be used much longer either due to their change to paid plans.
If you go to the file app/Http/Kernel.php you will see the VerifyCsrfToken middleware defined: <?php [...] class Kernel extends HttpKernel { [...] protected $middlewareGroups = [ 'web' => [ [...] \App\Http\Middleware\VerifyCsrfToken::class, [...] ], [...] ]; [...] }
For Postman to work, you need to either send the correct CSRF header, or remove the need for it on your routes.
I'm assuming based on your screenshot your Dingo API routes are using API_PREFIX=api
in your .env
file.
Check the Laravel documentation on CSRF tokens for more information about those. The gist that @BM2ilabs suggested has some basics on how to find out what CSRF token you're using for local testing in your session to put into Postman.
If you don't want to use CSRF protection, you are correct in using the $except
property on the VerifyCsrfToken
middleware as per the Laravel documentation - this has also come up on Stack Overflow before. Tricky to troubleshoot that without seeing your Kernel
and the full middleware file you're using. If the $except
property really isn't working for you, you can always override the VerifyCsrfToken::handle()
method as per this post and add whatever route checks you like:
public function handle($request, Closure $next)
{
if ( ! $request->is('api/*'))
{
return parent::handle($request, $next);
}
return $next($request);
}
If you are only creating an API that is going to be stateless and not need CSRF protection, you could just comment out the usage of the VerifyCsrfToken
middleware in your Kernel
entirely (and possibly some of the other session middleware), although I would recommend using some kind of authentication/validation that your visitor should be allowed to access the API endpoint.
You just need to add csrf_token
to the post , but that might be tricky with postman
in laravel add the header with what you use For example Axios :
it already has that integrated
jQuery Ajax
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
for more info's
Update
After some searching i found this article that show how to make csrf work with POSTMAN
as well
Gists of @ethanstenis
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With