Hello I have created an API and front end using React and doing request with axios. I added an authorization header with axios and I authorized it with php server-side but when the browser send the POST request, there is no Authorization Header.
// Tasks.js
const httpClient = axios.create({baseURL: "http://localhost/cpp/api/v1/"})
const resp = await httpClient.post("/work/createWork.php", {
body: {
taskid: id,
title: data.name,
content: data.content,
date: data.date,
matiere: data.matiere,
userID: 3
},
headers: {
'Authorization': "XXXXXXXX"
}
});
// createWork.php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: OPTIONS, POST");
header("Access-Control-Allow-Header: Content-Type, Authorization");
header("Access-Control-Max-Age: 86400");
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(204);
exit;
}
include './../main.php';
global $db;
$rq = new APIRequest("POST", true);
header("Content-Type: application/json");
if (!isset($_SERVER['HTTP_Authorization'])) {
echo "{\"error\": \"This endpoint need auth.\"}";
http_response_code(403);
return;
}
ERROR : POST http://localhost/cpp/api/v1/work/createWork.php [HTTP/1.1 403 Forbidden 19ms]