Reputation: 10502
I have this HTTP request interceptor:
var app = angular.module('mobApp.services');
app.factory('httpRequestInterceptor', function($q, $injector) {
return {
request: function(config) {
var utils = $injector.get('Utils'),
userDetails = utils.getCurrentUserDetails();
config.headers.Authorization = "Token 12309123019238";
if(userDetails) {
config.headers['X-AccessKey'] = userDetails.apiKey;
config.headers['X-UserId'] = userDetails.id;
}
return config;
}
}
});
app.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('httpRequestInterceptor');
}]);
It add X-
header to all GET/POST request except to a image GET request. why?
Upvotes: 0
Views: 1980
Reputation: 2330
my guess is that you want to intercept the request from a
<img ng-src="someurl"/>
tag
In this case you need to replace ng-src="someurl"
with http-src="someurl"
Otherwise these will not be fetched with $http
and thus not pass your interceptor
to make this work you need a custom directive:
app.directive('httpSrc', [
'$http', function ($http) {
var directive = {
link: link,
restrict: 'A'
};
return directive;
function link(scope, element, attrs) {
var requestConfig = {
method: 'Get',
url: attrs.httpSrc,
responseType: 'arraybuffer',
cache: 'true'
};
$http(requestConfig)
.success(function(data) {
var arr = new Uint8Array(data);
var raw = '';
var i, j, subArray, chunk = 5000;
for (i = 0, j = arr.length; i < j; i += chunk) {
subArray = arr.subarray(i, i + chunk);
raw += String.fromCharCode.apply(null, subArray);
}
var b64 = btoa(raw);
attrs.$set('src', "data:image/jpeg;base64," + b64);
});
}
}
]);
Upvotes: 1