I've been attempting to include an authorization header in my requests, but I'm facing some issues.
Here is the code I am using:
var config = {headers: {
'Authorization': token
}
};
return $http.get('http://localhost:3000/apis/users/all', config);
I also tried this approach:
$http.defaults.headers.common['Authorization'] = token;
However, in both cases, the headers that are sent to the back-end contain additional information like:
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:es-ES,es;q=0.8,ca;q=0.6,en;q=0.4,gl;q=0.2
Access-Control-Request-Headers:accept, authorization
Access-Control-Request-Method:GET
Connection:keep-alive
Host:localhost:3000
Origin:http://localhost:8000
Referer:http://localhost:8000/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36
What I actually need is a header that looks like this:
Authorization: token
Instead, I end up with:
Access-Control-Request-Headers:accept, authorization
As a result, the token value is not being included anywhere.
For my back-end, I am using expressjs and implementing CORS like so:
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
next();
});
Interestingly, when testing with the chrome extension Advance Rest Client, everything works fine. The request header does include "Authorization: valueOfToken".
Thank you for your assistance.