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
PHP Function That Creates a Nested Ul Li
PHP Datetime() Class, Change First Day of the Week to Monday
What Is the "" Symbol in Url Used for in PHP
Errorbag Is Always Empty in Laravel 5.2
Doctrine - How to Print Out the Real SQL, Not Just the Prepared Statement
What's the Difference Between Array_Merge and Array + Array
Reflectionexception: Class Classname Does Not Exist - Laravel
Loadhtml Libxml_Html_Noimplied on an HTML Fragment Generates Incorrect Tags
How to Convert Iso8601 to Date Format in PHP
Detect If Android App Has Been Installed on the Device Using a Mobile Web Page - PHP and Js
Protection Against Xss Exploits
Proper Prevention of Mail Injection in PHP
PHP How to Retrieve Array Values
PHP Password_Verify() Hash and Pass Won't Match
Variable Type Hinting in Netbeans (Php)
Getting the Name of a Child Class in the Parent Class (Static Context)
Can't Decrypt Using Pgcrypto from Aes-256-Cbc But Aes-128-Cbc Is Ok