'Can´t fetch data from laravel api by react JS - status of 419 (unknown status), postman working
I am trying to get data from laravel api by fetching it. In postman it`s working even with authorization with sanctum.
When I fetch with post method it gives:
Failed to load resource: the server responded with a status of 419 (unknown status)
and then:
message: "CSRF token mismatch." ...
Here is my api routes file:
Route::post('/posts', [PostController::class, 'store']);
Route::get('/posts', [PostController::class, 'index']);
Route::get('/posts/{id}/edit', [PostController::class, 'edit']);
Route::post('/register', [AuthController::class, 'register']);
Route::post('/login', [AuthController::class, 'login']);
And fetching:
export const addPost = async (post) => {
let resp = await fetch(`http://127.0.0.1:8000/api/posts`, {
method: "POST",
body: JSON.stringify(post),
headers: { Accept: "application/json", "Content-Type": "application/json" },
});
let jsData = await resp.json();
return jsData;
};
Thanks for help
Solution 1:[1]
if your APP_ENV is 'local' it will work in postman even if you dont put csrf token in the header..
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | abderrahman turki |