Over a million developers have joined DZone.

AngularJs Interceptor - Example

· DevOps Zone

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs

Here I will explain an angularjs interceptor to get application context path for every http request.

angular.module('app').factory('PathInterceptor', ['$location',function($location) {  
    var path = {
        request: function(config) {
        	
        	var path = $location.absUrl(); 
            var pathArray = path.split('/');
            var appContext = pathArray[3];
        	
        	config.url = "/"+appContext+config.url;
  
            return config;
        },
        response: function(response) {
        	
   
            return response;
        }
    };
    return path;
}]);

angular.module('app').config(['$httpProvider', function($httpProvider) {  
    $httpProvider.interceptors.push('PathInterceptor');
}]);

We are using angularjs $location service to get the context path and modify the http request.


Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}