It is common to encounter 419 page expired or status code unknown error in Laravel 11, 10, 9, 8, 7, this error occurs when the Laravel application is not able to verify the CSRF token security with forms or ajax requests.
When we created the form in our Laravel application and submitted it to the server using Postman, Ajax requests and common form submission techniques we faced 419 page expired or status code unknown error.
We have found some solutions to fix this error and this error is fixed, you can use these common solutions to fix the error.
How to Fix 419 Page Expired in Laravel 11,10, 9, 8
Here are some solutions to fix error 419 page expired laravel 11,10, 9, 8; as follows:
Solution 1 – 419 Page Expired Laravel Submit Form Login, Registration,etc
To fix 419 page timeout error in Laravel submit form, simply add @csrf
with your Laravel login, registration, etc forms. Simply open your login or registration form view file and add CSRF token
security to your form view file on the head section:
<form method="POST" action="/profile"> @csrf <!-- add csrf field on your form --> ... </form>
Solution 2 – 419 Page Expired Laravel Ajax
To fix 419 page expired Laravel Ajax, Make sure you include the CSRF token in your Ajax request headers or data in laravel.
Here’s an example of how to add the CSRF token in your Ajax request headers on laravel, Simply open your Ajax request code and add csrf token
into it; like following:
$.ajax({ type: "POST", url: '/your_url', data: { somefield: "Some field value", _token: '{{csrf_token()}}' }, success: function (data) { console.log(data); }, error: function (data, textStatus, errorThrown) { console.log(data); }, }); OR $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } });
Solution 3 – 419 Page Expired Laravel Postman
When encountering a 419 page expired laravel Postman, it usually indicates an issue with CSRF (Cross-Site Request Forgery) protection. Laravel’s default CSRF security requires a valid CSRF token to be included with every POST request.
To resolve 419 page expired laravel postman error, you need to follow the following steps:
If you prefer to keep CSRF protection enabled, you need to include the CSRF token with your POST requests in Postman. Here’s how:
- Send a GET request to the endpoint you’re testing in Postman. Make sure it’s a route that requires authentication.
- Inspect the response headers for the
Set-Cookie
header. It should contain a cookie namedXSRF-TOKEN
with the CSRF token value. - Copy the value of the
XSRF-TOKEN
cookie. - In Postman, go to the Headers tab for your POST request.
- Add a new header with the key
X-XSRF-TOKEN
and paste the CSRF token value as the header value. - Send the POST request again, and the “419 Page Expired” error should be resolved.
Solution 4 – Remove CSRF protection on specific URL
To disable CSRF protection field for all routes group or specific routes in laravel, Navigate to app\Http\Middleware\
directory and open VerifyCsrfToken.php
file and add the following lines of code in it:
<?php namespace App\Http\Middleware; use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier; class VerifyCsrfToken extends BaseVerifier { protected $except = [ 'payment/*', // routes group 'specific-route', // specific route ]; }
Conclusion
That’s all; you have learned how to fix the laravel 419 page expired error in laravel 11, 10, 9, 8, and 7 versions.