3

I'm trying to use an interceptor to add an 'Authorization' header to all my GET/POST requests. Here is the interceptor:

myApp.factory('httpRequestInterceptor', ['$rootScope', function ($rootScope) {
  return {
    request: function ($config) {
        $config.headers['Authorization'] = 'Basic ' + $rootScope.apiKey;
        return $config;
    }
  };
}]);

The interceptor is being used in the main module as follows in the myApp.config part:

$httpProvider.interceptors.push('httpRequestInterceptor');

For some reason I can't see the Authorization header in the network tab(I'm using Chrome) and it's not getting to the server and I don't get any errors. The GET/POST request in my application are to a remote server that is NOT in my domain.

Any idea what am I doing wrong here?

Solution: I found the problem, it was on the server side - apparently if I set the Access-Control-Allow-Headers in the response to * it doesn't work but if I specify the headers literally it works just fine.

4 Answers 4

1

the problem was on the server side - apparently setting the Access-Control-Allow-Headers in the response to * didn't work but specifying the headers literally to what i needed(Access-Control-Allow-Headers: Authorization) works just fine.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use:

myApp.run([
  '$http', '$rootScope',
  function($http, $rootScope) {
    $http.defaults.headers.common['Authorization'] = 'Basic ' + $rootScope.apiKey;
  }
]);

4 Comments

Strange I am using something like this in my current application and it work. Here is a jsfiddle that works: jsfiddle.net/epnxmkwy I can see in the chrome tools that is sends the header: Access-Control-Request-Headers:accept, authorization
When do you set the $rootScope.apiKey?
i found a solution, see the edited question. thanks for the help.
Good to know that you found the problem
0

I think you should change it to

 myApp.factory('httpRequestInterceptor', ['$rootScope', function ($rootScope) {
    var injector = {
        request: function (config) {
               config.headers['Authorization'] = 'Basic ' + $rootScope.apiKey;
               return config;
        }
    };
   return injector;
    } ]);

try like this website

2 Comments

@user1531186 have you tried the solution i have edited?
i found a solution, see the edited question. thanks for the help.
0

You can always tell angular to add the header using the $httpProvider like this:

myApp.config(['$httpProvider','$rootScope', function ($httpProvider, $rootScope) {
    $httpProvider.defaults.headers.common['Authorization'] = 'Basic' + $rootScope.apiKey;
}]);

This will add the header to ALL requests. For get and post, you can set the : $httpProvider.defaults.headers.get['Authorization'] = ''; and $httpProvider.defaults.headers.post['Authorization'] = ''; instead of $httpProvider.defaults.headers.common['Authorization'] if you don't want the header to be added to all requests.

3 Comments

tried this as well, its not working. also made sure i have Access-Control-Allow-Headers header on the response and its set to *.
@user1531186 Could you try creating a fiddle to reproduce the issue?
i found a solution, see the edited question. thanks for the help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.