user image

Jo Micheal
Published in : 2022-08-03

Laravel API POST method returns 419

Laravel

Hello everybody,
I have a problem in my API Laravel 9x  it returns 419 Page Expired, However everything is fine with the request,

api.php looks like

<?phpuse Illuminate\Http\Request;use Illuminate\Support\Facades\Route;use App\Http\Controllers\authentication\UsersController;Route::controller(UsersController::class)->prefix('/user')->group(function (){ Route::post('/signup', 'create');});

Any Idea why this happen?

 

 

 

 

web-brackets.com

Comments

Mohamed Atef Date : 2022-08-03

Best answers

51

Best answers

51

Welcome Back Jo,
This issue usually happens in the new Laravel setup because you need to exclude the API routes from the middleware of CSRF verification.
you will find a file called VerifyCsrfToken.php in app/Http/middleware/VerifyCsrfToken.php you should edit the file and update the expect array to include “api/*” like this 

<?phpnamespace App\Http\Middleware;use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;class VerifyCsrfToken extends Middleware{ /** * The URIs that should be excluded from CSRF verification. * * @var array<int, string> */ protected $except = [ 'api/*' ];}

Good Luck

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

API CORS error in Laravel 9x
Publish date: 2022-08-03 | Comments: 2
[solved] Nginx 413 Request Entity Too Large - Laravel
Publish date: 2022-02-01 | Comments: 7
I want to make multiple foreign keys in Laravel, can I?
Publish date: 2022-01-27 | Comments: 5
How to pass valiables to 404 error page in laravel?
Publish date: 2022-02-21 | Comments: 2