Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

AngularJs Interceptor - Example

DZone's Guide to

AngularJs Interceptor - Example

· DevOps Zone ·
Free Resource

Can you release faster without sacrificing quality? See how with our free ebook Strategies for a Successful Test Automation Project and a free trial of Ranorex Studio today!

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.


Get your test automation project off to the right start. Download your free test planning template and a 30-day no-obligation trial of Ranorex Studio today!

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}