3

I am new to angular and trying to make an API call with access-token. Below is the service that i am using for this API call.

(function(){
    angular.module('app')
        .factory('Student',Student);
        Student.$inject = ['$resource','$rootScope'];
        function Student($resource, $rootScope){ 
            var URL = 'myUrl';
            var details = $resource(URL + ':id/',{
                id:'@id',
            },{ }
        ); 
        return {
            details: details,
        };
    }
})();

Inside the controller

Student.details.get(function(data){

        console.log(data);

    });

My question is, how should i add an access-token with the header of this request? I am having the token in $rootScope. Descriptions shown here and here doesn't works for me.

Community
  • 1
  • 1
Hari Krishnan
  • 5,992
  • 9
  • 37
  • 55

1 Answers1

3

To add a header to your resource you can do something like this:

var details = $resource('test', {
    id: '@id',
}, {
    get: {
        headers: {
            'Authorization': 'dfgasdfjhkjhk3452i34hpiuhasd9f435kjkad'
        }
    }
});
dfsq
  • 191,768
  • 25
  • 236
  • 258