Laravel API Returning 419 Status Code on POST and PUT Requests
When developing RESTful APIs with Laravel, POST and PUT requests may encounter a 419 status code. This error stems from Laravel's Cross-Site Request Forgery (CSRF) protection.
To address this issue, consider using api.php instead of web.php for defining your API routes, as CSRF protection is not enabled by default in api.php.
If you prefer to use web.php, you can exclude specific routes from CSRF protection by adding them to the $except array of the VerifyCsrfToken middleware:
<code class="php">namespace App\Http\Middleware; use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier; class VerifyCsrfToken extends BaseVerifier { /** * The URIs that should be excluded from CSRF verification. * * @var array */ protected $except = [ '/api/*', ]; }</code>
By excluding your API routes (/api/* in this example), you effectively disable CSRF protection for those requests.
For further reference and guidance, consult the official Laravel documentation on [CSRF Protection](https://laravel.com/docs/5.5/csrf).
The above is the detailed content of How to Handle 419 Status Code in Laravel POST and PUT API Requests?. For more information, please follow other related articles on the PHP Chinese website!