Monday, 29 January 2018

HTTP Interceptors In AngularJS

In a request and response communication of a HTTP call if we want to inject some custom logic the HTTP Interceptor comes into picture. HTTP Interceptors executes 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 purpose.

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 custom header, timestamp in the request /response, encrypt and decrypt the request and response information or manipulate the request and response data over the request cycles.

HTTP Interceptors are similar to custom HTTP Module of ASP.NET.

There are four kinds of interceptors in AngularJS - Request, requestError, response, responseError

function testInterceptor() {
  return {
    request: function(config) {
      return config;

    requestError: function(config) {
      return config;

    response: function(res) {
      return res;

    responseError: function(res) {
      return res;

angular.module('app', [])
.factory('testInterceptor', testInterceptor)
.config(function($httpProvider) {
.run(function($http) {
    .then(function(res) {

        <title>$http interceptors</title>
    <script src=""></script>
        <script src="app.js"></script>
    <body ng-app="app">

An interceptor is simply a factory() service that returns an object with 4 properties that map to functions:

request: called before a request is sent, capable of mutating the request object
response: called with an $http request succeeds, is passed the results object,
responseError: called if an $http method fails

This object is then registered as an interceptor with the $httpProvider in a config() block. It's perfectly fine to include all or only a subset of the properties that are needed.

The way to think about interceptor functions is as promise callback functions that are called for all HTTP requests.


  1. Hi There,

    Gratitude for putting up this prolific article! You truly make everything a cake walk. Genuinely good stuff, saving time and energy.

    How to clear the cache memory or disable the cache option in browser programmatically using angularjs and javascript without using session or timestamp.

    Super likes !!! for this amazing post. I thinks everyone should bookmark this.

    Many Thanks,
    Irene Hynes



ADFS (1) ADO .Net (2) Ajax (1) Angular (1) Angular Js (17) Angular2 (28) ASP .Net (14) Authentication (1) Azure (1) Breeze.js (1) C# (50) CD (1) CI (2) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DevOps (4) DI (4) Dotnet (22) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (8) Lamda (3) Linq (11) Mongodb (1) MVC (50) NodeJS (7) RDLC (1) Report (1) SDLC (1) Sql Server (30) SSIS (3) SSO (1) SSRS (2) UI (1) WCF (13) Web Api (11) Web Service (1) XMl (1)