[英]Custom http headers for all actions from $resource
I have troubles finding in the docs, something that allows me to define my custom http headers for all the actions of a specific $resource, Did you know how to do this? 我在文档中找不到麻烦,这使我可以为特定$ resource的所有操作定义我的自定义http标头,您知道如何执行此操作吗?
This is what I want to avoid 这就是我要避免的
return $resource(main.dev + '/api/:entity/:action', '', {
SaveUser: {
method: 'POST',
params: {
entity: 'users',
action: 'register'
},
headers: {
api_key: main.api_key //same header
}
},
get:{
headers: {
api_key: main.api_key //same header
}
}
});
You can do it with a httpinterceptor like so: 您可以像这样使用httpinterceptor进行操作:
define(['./app'], function (app) {
'use strict';
return app.factory('requestInterceptor', ['$log', function ($log) {
var requestInterceptor = {
request: function (config) {
$log.debug(config);
//YOUR CODE HERE
//if config.url starts with /api
//config.headers.push token
return config;
}
};
return requestInterceptor;
}]).config(['$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push('requestInterceptor');
}]);
});
When config.url
starts with api you insert your headers into config.headers
. 当config.url
以api开头时,您可以将标头插入config.headers
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.