In a request and response communication of an HTTP call if we want to inject some custom logic the HTTP Interceptor comes into the picture. HTTP Interceptors execute some custom logic before or after the HTTP call.
For example, appending the authentication token to every HTTP request and that token is generated from the client who needs to be validated at the server for security purposes.
These interceptors act as a hook-up for the HTTP calls.
HTTP Interceptors are used for adding custom logic for authentication, authorization, session/state management, logging, modifying Response, URL rewriting, Error handling, Caching, adding a custom header, and time stamp in the request /response, and decrypting the request and response information or manipulate the request and response data over the request cycles.

HTTP Interceptors are similar to the custom HTTP Module of ASP.NET.
There are four kinds of interceptors in AngularJS - Request, requestError, response, responseError
Syntax for creating HTTP interceptors
var Interceptor = function ($q) {
return {
request: function (config) {
return config;
},
requestError: function (rejection) {
return $q.reject(rejection);
},
response: function (result) {
return result;
},
responseError: function (response) {
return $q.reject(response);
}
};
};
interceptorApp = angular.module('interceptorApp', []).config(function ($httpProvider) {
$httpProvider.interceptors.push(Interceptor);
});
Let us see what these interceptors are and how they are used.
Request
This interceptor is called before $http sends the request to the server. This function takes the request configuration object as an input parameter and returns a configuration object.
When there is a need to add, modify, or remove data prior to sending it to the server for each request the request functions of the interceptors are used.
For example
Let us consider whether we want to insert the authentication token into each request.
request: function (config) {
console.log('request started...');
// Validating the requests and assigning the csrf token to each request
var token = $cookieStore.get("auth");
config.headers['x-csrf-token'] = token;
return config;
}
requestError
This interceptor gets called when a request interceptor throws an error or is resolved with a rejection code.
For example
Let us say some error has happened when request interceptors fail and it has blocked the HTTP call, and then log the rejection.
requestError: function (rejection) {
console.log(rejection);
// Contains the data about the error on the request and return the promise rejection.
return $q.reject(rejection);
}
Response
This interceptor is called when the $http receives the response from the server.
This function receives a response object as a parameter to return a response object or a promise. A response interceptor is used to modify the response data or add a new set of values, calling another module or service call.


Sivaraj SPosted Jan 3, 2019, 4:30 AM
Can we set a request header for image tag url using the interceptor
mahesh pandianPosted Sep 7, 2017, 8:23 PM
A very good explanation. keep it up god work
Vishal PrajapatiPosted Feb 14, 2017, 11:34 PM
Informative one.....................
Asfend YarPosted Feb 29, 2016, 2:14 PM
good
vishnu kumar repakulaPosted Dec 10, 2015, 1:42 AM
Good one very use full
Debasis SahaPosted Nov 23, 2015, 7:10 AM
Good One..
Manas MohapatraPosted Nov 23, 2015, 4:17 AM
Informative one..
Sibeesh VenuPosted Nov 23, 2015, 4:09 AM
Nice Share
Harshad PansuriyaPosted Nov 23, 2015, 2:54 AM
Nice one
Humayun Kabir MamunPosted Nov 23, 2015, 12:20 AM
Nice...
Mukesh KumarPosted Nov 22, 2015, 11:43 PM
Good One