0

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]

  • The client is sending the header `Authorization`, but the server is checking for the header `HTTP_Authorization`. – outlaw Aug 11 '23 at 13:10
  • @outlaw It looks to be the correct way: [How do I read any request header in PHP](https://stackoverflow.com/a/541463/2257664). But it should be in uppercase though. – A.L Aug 11 '23 at 13:12
  • @A.L whoops, thanks! I always use `getallheaders()` and didn't realize they were stored in `$_SERVER` with the prepended `http`! – outlaw Aug 11 '23 at 13:19
  • 2
    For one thing, your code contains a typo: `Access-Control-Allow-Header` should be `Access-Control-Allow-Headers` (plural). This is one reason why I recommend the use of an existing CORS middleware library rather than that of a middleware written from scratch. – jub0bs Aug 11 '23 at 18:02

1 Answers1

0

JS

The Axios documentation shows that headers are provided as the third parameter of the post method (indentation added so that's it's easier to see):

const {data} = await axios.post(
    '/user',
    document.querySelector('#my-form'),
    {
        headers: {
            'Content-Type': 'application/json'
        }
    }
)

So you have to remove headers that is next to body and pass it as the third parameter:

const httpClient = axios.create({baseURL: "http://localhost/cpp/api/v1/"})
const resp = await httpClient.post(
    "/work/createWork.php",
    {
        taskid: id,
        title: data.name,
        content: data.content,
        date: data.date,
        matiere: data.matiere,
        userID: 3
    },
    {
        headers: {
            'Authorization': "79619bca-225b-4f28-b337-43d37bbb5048"
        }
    },
);

The body isn't shown in the documentation, I removed it.

PHP

if (!isset($_SERVER['HTTP_Authorization'])) {

should be:

if (!isset($_SERVER['HTTP_AUTHORIZATION'])) {

See How do I read any request header in PHP.

A.L
  • 10,259
  • 10
  • 67
  • 98