在AngularJS上设置默认标头,但不要在一个特定请求上使用它

时间:2022-08-22 19:39:34

For sending OAuth2 token I am setting up defaults header on AngularJS like this:

为了发送OAuth2令牌,我在AngularJS上设置默认标头,如下所示:

$http.defaults.headers.common['Authorization'] = 'Bearer ' + access_token;

This works great but I don't need this header (I get an error) for one specific request.

这工作得很好但我不需要这个标题(我得到一个错误)一个特定的请求。

Is there a way of excluding defaults header when performing that request?

有没有办法在执行该请求时排除默认标头?

Thanks!

谢谢!

SOLVED

Thanks to Riron for getting me on a right path. Here's the answer:

感谢Riron让我走上了正确的道路。这是答案:

$http({
    method: 'GET',
    url: 'http://.../',

    transformRequest: function(data, headersGetter) {
        var headers = headersGetter();

        delete headers['Authorization'];

        return headers;
    }
});

4 个解决方案

#1


17  

When you make your call with $http, you can override defaults headers by providing them directly in your request config:

使用$ http拨打电话时,您可以直接在请求配置中提供默认值标头:

$http({method: 'GET', url: '/someUrl', headers: {'Authorization' : 'NewValue'} }).success();

Otherwise you could transform your request using the transformRequest parameter, still in your $http config. See doc :

否则,您可以使用transformRequest参数转换您的请求,仍然在$ http配置中。见文档:

transformRequest – {function(data,headersGetter)|Array.<function(data, headersGetter)>} – transform function or an array of such functions. The transform function takes the http request body and headers and returns its transformed (typically serialized) version.

transformRequest - {function(data,headersGetter)| Array。 } - 转换函数或此类函数的数组。 transform函数接受http请求主体和头部并返回其转换(通常是序列化)版本。 (data,headersgetter)>

This way you could delete an header for a single request before it's being send:

这样,您可以在发送之前删除单个请求的标头:

$http({method: 'GET', 
       url: '/someUrl', 
       transformRequest: function(data,headersGetter){ //Headers change here } 
}).success();

#2


8  

For latecomers, whilst the solution might have worked - you actually shouldn't need to use transformRequest for this.

对于后来者来说,虽然解决方案可能有效 - 但实际上您不需要为此使用transformRequest。

The Angular docs for the $http service actually have this exact situation covered:

$ http服务的Angular文档实际上涵盖了这种情况:

To explicitly remove a header automatically added via $httpProvider.defaults.headers on a per request basis, Use the headers property, setting the desired header to undefined. For example:

要基于每个请求显式删除通过$ httpProvider.defaults.headers自动添加的标头,请使用headers属性,将所需标头设置为undefined。例如:

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

 $http(req).success(function(){...}).error(function(){...});

#3


6  

Angular 1.4.0 can no longer modify request headers using transformRequest:

Angular 1.4.0无法再使用transformRequest修改请求标头:

If one needs to dynamically add / remove headers it should be done in a header function, for example:

如果需要动态添加/删除标题,则应在标题函数中完成,例如:

$http.get(url, {
  headers: {
    'X-MY_HEADER': function(config) {
      return 'abcd'; //you've got access to a request config object to specify header value dynamically
    }
  }
})

#4


0  

While the $httpProvider can override $http the use of intereceptors are 1 way of handling this, I end up doing it this way

虽然$ httpProvider可以覆盖$ http,但使用interceptors是处理这种情况的一种方法,我最终这样做了

function getMyStuff(blah) {

        var req = {
            method: 'GET',
            url: 'http://...',
            headers: {
                'Authorization': undefined
            }
        }
        return $http(req)
            .then(function(response) {
                return response.data;
            });


}

#1


17  

When you make your call with $http, you can override defaults headers by providing them directly in your request config:

使用$ http拨打电话时,您可以直接在请求配置中提供默认值标头:

$http({method: 'GET', url: '/someUrl', headers: {'Authorization' : 'NewValue'} }).success();

Otherwise you could transform your request using the transformRequest parameter, still in your $http config. See doc :

否则,您可以使用transformRequest参数转换您的请求,仍然在$ http配置中。见文档:

transformRequest – {function(data,headersGetter)|Array.<function(data, headersGetter)>} – transform function or an array of such functions. The transform function takes the http request body and headers and returns its transformed (typically serialized) version.

transformRequest - {function(data,headersGetter)| Array。 } - 转换函数或此类函数的数组。 transform函数接受http请求主体和头部并返回其转换(通常是序列化)版本。 (data,headersgetter)>

This way you could delete an header for a single request before it's being send:

这样,您可以在发送之前删除单个请求的标头:

$http({method: 'GET', 
       url: '/someUrl', 
       transformRequest: function(data,headersGetter){ //Headers change here } 
}).success();

#2


8  

For latecomers, whilst the solution might have worked - you actually shouldn't need to use transformRequest for this.

对于后来者来说,虽然解决方案可能有效 - 但实际上您不需要为此使用transformRequest。

The Angular docs for the $http service actually have this exact situation covered:

$ http服务的Angular文档实际上涵盖了这种情况:

To explicitly remove a header automatically added via $httpProvider.defaults.headers on a per request basis, Use the headers property, setting the desired header to undefined. For example:

要基于每个请求显式删除通过$ httpProvider.defaults.headers自动添加的标头,请使用headers属性,将所需标头设置为undefined。例如:

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

 $http(req).success(function(){...}).error(function(){...});

#3


6  

Angular 1.4.0 can no longer modify request headers using transformRequest:

Angular 1.4.0无法再使用transformRequest修改请求标头:

If one needs to dynamically add / remove headers it should be done in a header function, for example:

如果需要动态添加/删除标题,则应在标题函数中完成,例如:

$http.get(url, {
  headers: {
    'X-MY_HEADER': function(config) {
      return 'abcd'; //you've got access to a request config object to specify header value dynamically
    }
  }
})

#4


0  

While the $httpProvider can override $http the use of intereceptors are 1 way of handling this, I end up doing it this way

虽然$ httpProvider可以覆盖$ http,但使用interceptors是处理这种情况的一种方法,我最终这样做了

function getMyStuff(blah) {

        var req = {
            method: 'GET',
            url: 'http://...',
            headers: {
                'Authorization': undefined
            }
        }
        return $http(req)
            .then(function(response) {
                return response.data;
            });


}