restangular icon indicating copy to clipboard operation
restangular copied to clipboard

Default headers only sent for first request.

Open simpss opened this issue 9 years ago • 4 comments

Hi, This might be a dumb mistake on my part, but wanted to ask around a bit.

When setting the headers with:

Restangular.setDefaultHeaders({'Authorization': "Bearer " + authService.getToken()});

The header is only sent with the first request made, even when refreshing the same page the header isn't included again.

workaround with an interceptor. Also used this to see what's going on by printing out the headers.

app.run(function(Restangular, AuthenticationService){
        Restangular.addFullRequestInterceptor(function(element, operation, route, url, headers, params){
            var currentHeaders = headers;
            currentHeaders.Authorization = "Bearer " + AuthenticationService.getToken();

            return {
                element: element,
                params: params,
                headers: currentHeaders
            };
        });
    });

Restangular v1.4.0 - 2015-04-03 AngularJS v1.3.15

simpss avatar Apr 25 '15 23:04 simpss

Doesn't seem to work in 1.5.1 either.

jkytomaki avatar Aug 10 '15 12:08 jkytomaki

I am trying to do the same. It doesn't seem to work in 1.5.2 either.

vamsu avatar Jun 15 '16 02:06 vamsu

Try setting these defaults in the RestangularProvider:


app.config(function(RestangularProvider) {
  Restangular.setDefaultHeaders({'Authorization': "Bearer " + authService.getToken()});
})

See if that works. If not, please provide a Plunk or Fiddle detailing the issue

daviesgeek avatar Jun 17 '16 00:06 daviesgeek

old post but for me using addFullRequestInterceptor worked:

Restangular.addFullRequestInterceptor(function (element, operation, route, url, headers, params, httpConfig) {

            headers = {'Authorization': 'Bearer ' +  authService.getToken()};

            return {
                element: element,
                params: params,
                headers: headers,
                httpConfig: httpConfig
            };

        });

amacado avatar Nov 16 '16 15:11 amacado