HTTP Interceptor Service
The HTTP Interceptor Service is used in the preprocessing and the postprocessing of $http requests and responses in AngularJS.
The HTTP Interceptor Service is an AngularJS service that provides a convenient way to modify requests made by the $http service before the requests are sent and once they have been returned. You can use HTTP interceptors to add global logic for handling HTTP errors, for authentication, or for other synchronous or asynchronous preprocessing and postprocessing of requests and responses.
HTTP interceptors are service factories. You register them with the $httpProvider by adding them to the $httpProvider.interceptors array. The service factory is called by an interceptor factory and is injected with dependencies. The factory then returns the interceptor object, which contains the interceptor methods.
HTTP interceptors are event functions that are triggered during the lifecycle of an HTTP call. They run whenever a $http, $resource, or $httpBackend service is executed.
Setup
An HTTP interceptor is a simple factory service. It returns an object with a property that maps to a function. There are four possible properties:
- request: Is called before a request is made to the remote service so that you can modify the configuration and perform other actions. This function receives the request configuration object as a parameter and must return a configuration object or a promise.
- requestError: Is called when a request cannot be made or when a request is rejected by another interceptor.
- response: Is called with a successful response object when a $http request succeeds.
- responseError: Is called with a rejection object when a previous response interceptor returns an error or rejection.
The following code excerpt provides an example of the format of an interceptor service:
angular.module('InterceptorModule', [])
.factory('sampleInterceptor', function(){
return {
request: function(config) {
return config;
},
requestError: function(config) {
return config;
},
response: function(response) {
return response;
},
responseError: function(response) {
return response;
}
}
});
Registering Interceptors
Interceptors are registered with the $httpProvider by adding them to the $httpProvider.interceptors array. Interceptors are executed during the request phase in the order in which they appear in the array. In the response phase, they are executed in reverse order.
The following image illustrates the process:
The following code excerpt shows how to register an interceptor:
.config(function($httpProvider) {
$httpProvider.interceptors.push('sampleInterceptor');
});
Interceptors in the SmartEdit Framework
SmartEdit has a set of HTTP interceptors that can be extended; these HTTP interceptors are as follows:
- httpAuthInterceptor: Provides the functionality to perform global authentication by intercepting the requests before forwarding them to the server and responses before they are given to the application code.
- httpErrorInterceptor: Handles all response errors with the exception of 401 - Unauthorized Access Error or OCC validation errors.
- i18nInterceptor: Intercepts all i18n requests and replaces the i18nAPIRoot in the URL with a URL provided in the configuration.