在每次 $http 调用时为 header 生成唯一值
Generating a unique value for a header on each $http call
我有一个相当大的 AngularJS 应用程序,出于日志记录的目的,我的任务是向来自应用程序的所有包含 的 HTTP 请求添加自定义 header每个请求的唯一 ID。 这对我们的 API 调用确实更有价值,但截至目前,我只是针对所有请求(获取模板、样式等)
我目前正在使用提供者装饰器来修补 $HttpProvider
公开的每个方法(基于 this post 的实现),以尝试在每次其中一个 $http
方法运行,并添加适当的 header:
module.config([
'$provide',
function ($provide) {
$provide.decorator('$http', [
'$delegate',
function addUniqueIdHeader($http) {
var httpMethods = ['get', 'post', 'put', 'patch', 'delete'];
/**
* Patched HTTP factory function that adds a request ID each time it is called.
* @param {string} method - A valid HTTP method.
* @return {function} A function that sets various request properties.
*/
function httpWithHeader(method) {
return function(url, data, config) {
config = config || {};
config.headers = config.headers || {};
// the magic
config.headers['My-Custom-Header'] = aUniqueId();
data = data || {};
config.method = method.toUpperCase();
// return `$http` with a modified config, adding the URL and data passed in
// `_.extend()` is lodash, not underscore.
return $http(_.extend(config, {
url: url,
data: data
}));
}
};
// back up the orginal methods and patch
_.each(httpMethods, function (httpMethod) {
var backupMethod = '_' + httpMethod;
$http[backupMethod] = $http[httpMethod];
$http[httpMethod] = httpWithHeader(httpMethod);
});
return $http;
}
]);
}
]);
到目前为止,我所拥有的东西有时会起作用,但似乎并不能始终如一地起作用(一些 API 请求有,有些则没有)。我应该注意,我们使用的是 AngularJS (1.0.6) 的相当旧的版本,不,我无法升级(尽管我很想升级),因此无法使用请求拦截器。此外,我们在大多数 API 交互中使用 Restangular。
我的问题是,使用提供者装饰器是正确的方法吗?如果是这样,是否有更简洁的方法来添加 header 而不必 override/patch 我忽略的每个单独的 HTTP 方法?
提前致谢。
我最终通过使用 Restangular 的 request interceptors 解决了我的问题,因为我们使用的 Angular 版本没有内置它们。
我有一个相当大的 AngularJS 应用程序,出于日志记录的目的,我的任务是向来自应用程序的所有包含 的 HTTP 请求添加自定义 header每个请求的唯一 ID。 这对我们的 API 调用确实更有价值,但截至目前,我只是针对所有请求(获取模板、样式等)
我目前正在使用提供者装饰器来修补 $HttpProvider
公开的每个方法(基于 this post 的实现),以尝试在每次其中一个 $http
方法运行,并添加适当的 header:
module.config([
'$provide',
function ($provide) {
$provide.decorator('$http', [
'$delegate',
function addUniqueIdHeader($http) {
var httpMethods = ['get', 'post', 'put', 'patch', 'delete'];
/**
* Patched HTTP factory function that adds a request ID each time it is called.
* @param {string} method - A valid HTTP method.
* @return {function} A function that sets various request properties.
*/
function httpWithHeader(method) {
return function(url, data, config) {
config = config || {};
config.headers = config.headers || {};
// the magic
config.headers['My-Custom-Header'] = aUniqueId();
data = data || {};
config.method = method.toUpperCase();
// return `$http` with a modified config, adding the URL and data passed in
// `_.extend()` is lodash, not underscore.
return $http(_.extend(config, {
url: url,
data: data
}));
}
};
// back up the orginal methods and patch
_.each(httpMethods, function (httpMethod) {
var backupMethod = '_' + httpMethod;
$http[backupMethod] = $http[httpMethod];
$http[httpMethod] = httpWithHeader(httpMethod);
});
return $http;
}
]);
}
]);
到目前为止,我所拥有的东西有时会起作用,但似乎并不能始终如一地起作用(一些 API 请求有,有些则没有)。我应该注意,我们使用的是 AngularJS (1.0.6) 的相当旧的版本,不,我无法升级(尽管我很想升级),因此无法使用请求拦截器。此外,我们在大多数 API 交互中使用 Restangular。
我的问题是,使用提供者装饰器是正确的方法吗?如果是这样,是否有更简洁的方法来添加 header 而不必 override/patch 我忽略的每个单独的 HTTP 方法?
提前致谢。
我最终通过使用 Restangular 的 request interceptors 解决了我的问题,因为我们使用的 Angular 版本没有内置它们。