Skip to content

Instantly share code, notes, and snippets.

@acroca
Last active December 21, 2015 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acroca/6227680 to your computer and use it in GitHub Desktop.
Save acroca/6227680 to your computer and use it in GitHub Desktop.
Hack for Angular.js to inject authentication headers based on url and request body (if needed).
// It works for v1.1.5.
// It's very fragile, so be careful using it with other versions.
//
// To understand how it works, check this out: https://github.com/angular/angular.js/blob/master/src/ng/httpBackend.js#L26-L31
//
// Tried and failed more elegant solutions:
// - Interceptors. Couldn't get the request body.
// - `transformRequest` for the resource or $http. Couldn't get the body or the URL.
app.config(function ($httpBackendProvider) {
originalBackend = $httpBackendProvider.$get[3];
newBackend = function (a,b,c) {
var originalBackendfunc = originalBackend(a,b,c);
return function(method, url, post, callback, headers, timeout, withCredentials, responseType) {
// Do your changes here
originalBackendfunc(method, url, post, callback, headers, timeout, withCredentials, responseType);
};
};
$httpBackendProvider.$get[3] = newBackend
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment