I'm having problems accessing the Authorization header when sending a GET request from URLRequest in Swift.
I've made a simple script that converts the $_SERVER parameter to JSON and outputs to the browser to test that I'm sending the Authorization header correctly.
Here's my PHP code:
echo json_encode($_SERVER);
exit;
Here's my .htaccess file
RewriteEngine On
RewriteBase /
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
RewriteCond %{HTTP:Authorization} ^(.+)$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
When I send a request via PHP or using the Postman application, I get the following response:
{
"TZ": "Etc/UTC",
"REDIRECT_REDIRECT_UNIQUE_ID": "WQOWeWALzYlxj8TEssOrHgAAACA",
"REDIRECT_REDIRECT_SCRIPT_URL": "/",
"REDIRECT_REDIRECT_SCRIPT_URI": "/",
"REDIRECT_REDIRECT_HTTP_AUTHORIZATION": "12345678",
"REDIRECT_REDIRECT_HTTPS": "on",
"REDIRECT_REDIRECT_SSL_TLS_SNI": "/",
"REDIRECT_REDIRECT_STATUS": "200",
"REDIRECT_UNIQUE_ID": "WQOWeWALzYlxj8TEssOrHgAAACA",
"REDIRECT_SCRIPT_URL": "/",
"REDIRECT_SCRIPT_URI": "/",
"REDIRECT_HTTP_AUTHORIZATION": "12345678",
....etc....
The REDIRECT_REDIRECT_HTTP_AUTHORIZATION and REDIRECT_HTTP_AUTHORIZATION parameters return the value passed in the Authorisation header as expected.
When I try the same request using URLRequest in Swift the REDIRECT_REDIRECT_HTTP_AUTHORIZATION parameter is empty.
Here's my Swift code:
var url = URL(string: urlString)!
var request : URLRequest!
request.httpMethod = "GET"
request.setValue(auth, forHTTPHeaderField: "Authorization")
request.setValue("test", forHTTPHeaderField: "Hello")
URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
do {
guard let responseData = data else {return}
guard let jsonData = try JSONSerialization.jsonObject(with: responseData, options: []) as? NSDictionary else {return}
print(jsonData)
I get the following response:
{
. . .
"HTTP_HELLO" = test;
"REDIRECT_HANDLER" = "application/x-httpd-ea-php56";
"REDIRECT_HTTPS" = on;
"REDIRECT_REDIRECT_HTTPS" = on;
"REDIRECT_REDIRECT_HTTP_AUTHORIZATION" = "";
"REDIRECT_REDIRECT_SCRIPT_URI" = "/";
"REDIRECT_REDIRECT_SCRIPT_URL" = "/";
"REDIRECT_REDIRECT_STATUS" = 200;
"REDIRECT_URL" = "/api/index.php";
"REMOTE_ADDR" = "149.12.15.210";
"REMOTE_PORT" = 64235;
"REQUEST_METHOD" = GET;
"REQUEST_SCHEME" = https;
"REQUEST_TIME" = 1493407944;
. . .
}
The REDIRECT_REDIRECT_HTTP_AUTHORIZATION parameter is empty.
I can see that the HTTP_HELLO header is set to "test" so the problem doesn't seem to be in the Swift code.
This issue only seems to affect requests sent using URLRequest in Swift.
I assume the problem is on the server side and possibly in the .htaccess file but I can't find a way to get this working correctly.
The server runs CentOS 7 WHM/cpanel. I'm able to make changes to the WHM/cpanel settings if required.
Any help would be appreciated as I can't see what could cause the REDIRECT_REDIRECT_HTTP_AUTHORIZATION to be empty.
Thanks.
