Search code examples
angularjshttp-headersrestangular

Restangular response.headers always null or undefined


I'm using Restangular on my angularjs app.

I'm issuing a request that gives me some response headers I need to retrieve. I'm using RestangularConfigurer.setFullResponse(true) so I can access response.headers. The problem is I'm getting null or undefined for every value I'm trying to retrieve.

Here's the code.

In the controller:

auth.login($scope.user).then(function(response) {
  console.log(response.headers('myHeader')); // This gives me null
  auth.saveToken(response.headers.myHeader); // Here I retrieve the header
  $state.go('users.list');
  $translate('AUTH.LOGINSUCCESSMESSAGE').then(function(message) {
    logger.logSuccess(message);
  });
}, function(response) {
  console.log('Error logging in, with status: ' + response.status);
  $translate('AUTH.LOGINERRORMESSAGE').then(function(message) {
    logger.logError(message);
  });
});

In the service:

auth.login = function(user) {
    return MyRestangular.all('sessions').post(user);
};

My Restangular service configuration:

app.factory('MyRestangular', function(configuration, Restangular) {
    return Restangular.withConfig(function(RestangularConfigurer) {
        RestangularConfigurer.setBaseUrl(configuration.backendAPIURL);
        RestangularConfigurer.setFullResponse(true);
    });
});

Solution

  • Found the solution.

    Just make sure that the backend is exposing the needed headers with Access-Control-Expose-Headers.