POST with API-TOKEN does not work
GET with API-TOKEN works, normal POST and GET also works. Stops working when I try to send POST together with token and data. I also tried not using localhost from https://rynokua.com
Fetch:
const response = await fetch(`${this._apiBase}cars/add`, {
method: 'POST',
headers: {
'X-API-TOKEN': this._apiHeaders.tokenApi,
'Content-Type': 'application/json',
},
body: JSON.stringify({
city_id: data.city,
color_id: 39,
category_id: data.category,
body_id: data.body,
fuel_id: data.fuel,
drive_id: data.drive,
race: data.race,
description: data.aboutPost,
gearbox_id: data.gearbox,
}),
})
cakephp application.php
->add(function ($request, $response, $next) {
$http_origin = '*';
if (isset($_SERVER['HTTP_ORIGIN']))
$http_origin = $_SERVER['HTTP_ORIGIN'];
else {
if (isset($_SERVER['HTTP_REFERER'])) {
$http_origin = $_SERVER['HTTP_REFERER'];
}
}
if ($http_origin == "https://rynokua.com" || $http_origin == "http://localhost:3000") {
return $next($request, $response)
->withHeader('Access-Control-Allow-Origin', $http_origin)
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, DELETE, OPTIONS')
->withHeader('Access-Control-Allow-Headers', 'X-API-TOKEN, content-type')
->withHeader('Access-Control-Allow-Type', 'application/json');
}
return $next($request, $response);
}
error:
Access to fetch at ‘https://api.rynokua.com/api/cars/add’ from origin ‘http://localhost:3000’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: Redirect is not allowed for a preflight request.
post-service.js:7
POST https://api.rynokua.com/api/cars/add net::ERR_FAILED
