Send request without adding header in angular js

I am trying to submit a request to a third party service. for this I need to remove the default 'x-access-token' header. For this, the following was done below

$http({
    url: 'http://ip-api.com/json',
    method: 'GET',
    transformRequest: function(data, headersGetter) {
          var headers = headersGetter();

          delete headers['x-access-token'];

          return headers;
        }
  }).then(function(res){
    console.log(res);
  },function(error){
    console.log(error);
  });

      

Following this link .

But I am getting this error

TypeError: Cannot convert object to primitive value
at angular.js:10514
at sendReq (angular.js:10333)
at $get.serverRequest (angular.js:10045)
at processQueue (angular.js:14567)
at angular.js:14583
at Scope.$get.Scope.$eval (angular.js:15846)
at Scope.$get.Scope.$digest (angular.js:15657)
at Scope.$get.Scope.$apply (angular.js:15951)
at done (angular.js:10364)
at completeRequest (angular.js:10536)

      

+3


source to share


2 answers


The config object $http

allows you to override sending the HTTP header for a specific request. See Configuration Property Headers.

To explicitly remove the header automatically added through $httpProvider.defaults.headers

for every request, use the headers property by setting the desired header to undefined.

NOTE: Set the wish title / headers to undefined as follows and it will not affect global settings.

See example:



var req = {
 method: 'POST',
 url: 'http://example.com',
 headers: {
   'Content-Type': undefined
 },
 data: { test: 'test' }
}

$http(req).then(function(){...}, function(){...});

      

More documentation .

It can be a list of headers or a function that returns a list of headers. So to request a non auth header, make a copy of the default headers, remove the header you don't need and then execute the request.

Hope it helps.

0


source


"transformRequest" does not work the same as removing headers for individual requests as of angularjs 1.4. It is clear from the documentation that we should use headers instead of eg:

  $http({method: 'GET', 
               url: "url", 
               headers: {
                   'header-name': undefined
                 }
        }).success(function(data){console.log(data)});

      




0


source







All Articles