You'll have to make sure that the CSRF token is added to your POST request, did you do that?
Laravel API post method is not working but get is working in Nuxt
I have a Laravel project using Breeze. I am using cookie. I can login and I can get data from DB but when I want to post it gives me 419 CSRF Token Mismatch error.
This is my Laravel .env configuration:
APP_URL=http://localhost:8000 FRONTEND_URL=http://localhost:3000 SESSION_DOMAIN=localhost SANCTUM_STATEFUL_DOMAINS=localhost:3000
My model:
class Request extends Model { use HasFactory;
protected $fillable = [ 'name', 'email', ]; }
My api.php file:
Route::get('v1/requests', [RequestController::class, 'index'])->middleware(['auth:sanctum']); Route::post('v1/requests', [RequestController::class, 'store'])->middleware(['auth:sanctum']);
and this is my Nuxt post request:
return client('/api/v1/requests', { method: 'POST', body: formData, headers: { 'Accept': 'application/json', } })
The response in browser is:
{ "message": "CSRF token mismatch.", "exception": "Symfony\Component\HttpKernel\Exception\HttpException", "file": "D:\Projects\2024\2- Megafon test\backend-test\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Exceptions\Handler.php", "line": 492, "trace": [ .... }
I want that if get request and login works, then why post give "message": "CSRF token mismatch.", error?
Please or to participate in this conversation.