restangular
restangular copied to clipboard
Default headers only sent for first request.
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
Doesn't seem to work in 1.5.1 either.
I am trying to do the same. It doesn't seem to work in 1.5.2 either.
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
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
};
});