angularjsheaderangular-http-interceptorscustom-headers

Invalid header name angularJS


I am trying to implement HTTPInterceptor to add custom headers in all out-going request to avoid duplicate post requests .

But I get the following error:

Error: Invalid header name.

Goal

To avoid duplicate post request by adding unique request ids in all out going requests.

What I tried

I had already developed the interceptor to display loading and added the custom header in request section but was getting the same error , so tried to create one more factory for httprequestinterceptor which will add custom header .

angular.module('cinemaBooking').
factory('httpRequestInterceptor', function ($injector) {
      return {
        request: function (config) {    
      var generalService= $injector.get('generalService');
              config.headers['webReqId'] = 'BookingM-'+generalService.generateRandomID;
          return config;
        }
      };
    });

I need to add webReqId for all out-going post requests.

Thanks.


Solution

  • Looks like generalService.generateRandomID is a function, if it is, you need to execute it:

    ...
      config.headers['webReqId'] = 'BookingM-'+generalService.generateRandomID(); //<--
    ...