Authorization Header Missing in PHP Post Request

Authorization header missing in PHP POST request

Somehow, the Authorization header was stripped away. By adding the following lines in my .htaccess, I was able to get it to work.

RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

Authorization' header sent with request, but missing from apache_request_headers()

After some quick search found setting a rewrite rule works

 <IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

Can anyone tell me what it does ?

Troubleshooting missing Authorization request header in PHP

The problem appears to have been indeed related to CORS and after trying a multitude of approaches, the following solution is now working.

Replacing my headers in read.php with:

// Allow from any origin
if (isset($_SERVER['HTTP_ORIGIN'])) {
// Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one
// you want to allow, and if so:
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}

// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
// may also be using PUT, PATCH, HEAD etc
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");

if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

exit(0);
}

Credit goes to slashingweapon who used it to answer CORS with php headers

php rest curl authentication header items missing

I've solved the problem with putting the API key as 'x-api-key' in the HTTP Header.

This looks like the following code:

$api_key = 'abc';
$token = 'xyz';

$authorization = 'Authorization: Bearer ' . $token;
$api = 'x-api-key: ' . $api_key;
curl_setopt($curl, CURLOPT_HTTPHEADER, array($authorization, $api));


Related Topics



Leave a reply



Submit