-1

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",

sabbir
  • 2,020
  • 3
  • 26
  • 48

2 Answers2

0

You're adding token as a request header, not as a property in the query string. Use the second (in your example empty) object to add parameters, or add them directly in your controller:

user.getUser({token: 'sometoken'}, {}, function(res) {

}, function(err) {

});
Norbert Huurnink
  • 1,326
  • 10
  • 18
0

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

user2013
  • 538
  • 5
  • 14