Search code examples
angularjsangular-resource

header is not working on angular $resource


In my angular app, I want to use angular custom header. I'm using the following code::

Angular Factory

angular.module('app').factory('UserAPI', ['$resource', 'session',   function($resource, session) {
   var mainUrl = 'http://localhost:8006/dev' + '/users';
   return {
      getService : function() {
        var token = session.getToken();
        console.log(token); //token is printed here
        return $resource(mainUrl, { }, {
           getData: {
                method: 'GET',
                url: mainUrl + '/:userId/dashboard',
                isArray: true,
                headers: { 'Token': token }
            }
         });
      }
   }
}]);

Angular Controller

angular.module('app').controller('UserCtrl', ['$scope', 'UserAPI',  function($scope, UserAPI) {

    var user = UserAPI.getService();

    user.getData({ userId: 'some-user-id' }, {}, function(res) {

    }, function(err) {

    });
}]);

When I make call user.getUser(......), an url is generated as like as GET:: http://localhost:8006/dev/user/some-user-id/dashboard instead of GET:: http://localhost:8006/dev/user/some-user-id/dashboard?token=SomeVeryLongToken, I mean token is missing on api call, although I'm using headers: { 'Token': token } but still problem.

How can I solve this problem?

NB "angular": "^1.4.0", "angular-resource": "^1.4.0",


Solution

  • change headers: { 'Token': token } to params: { 'token': token }