Laravel API post method is not working but get is working in NUuxt

46 views Asked by At

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.

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',
      'Referer': 'http://localhost:3000',
    }
  })

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 the error:

"message": "CSRF token mismatch."

0

There are 0 answers