Angular Js – set token on header default

Instead of placing the token on the headers inside each service (or call), it might be better to use an $http interceptor (docs here).

Then, you can place the token on every request. This will work whether the request is a GET or POST.

JS sample:

$httpProvider.interceptors.push(function($q, $cookies) {
    return {
     'request': function(config) {

          config.headers['Token'] = $cookies.loginTokenCookie;
          return config;
      }
    };
  });

Leave a Comment