aboutsummaryrefslogtreecommitdiffstats
path: root/src/ng/http.js
diff options
context:
space:
mode:
authorbolasblack2013-07-03 15:51:05 +0800
committerIgor Minar2013-07-08 08:55:20 -0700
commita7150f1256f2a97a931b3c0d16eab70f45e81cae (patch)
treeb8c74445e77e116c7a8f0d726596ffc0ab40f2a1 /src/ng/http.js
parent0d124e190b24e737405b467d4fc11f10047b3d9c (diff)
downloadangular.js-a7150f1256f2a97a931b3c0d16eab70f45e81cae.tar.bz2
feat($http): accept function as headers value
So we can request with dynamic header value. module.factory('Res', [ '$resource' '$routeParams' 'globalConfig' function($resource, $routeParams, globalConfig) { resource('/url/:id', {id: "@id"}, { patch: { method: 'patch', headers: { 'Authorization': function() { return "token " + globalConfig.token; } } } }); }]);
Diffstat (limited to 'src/ng/http.js')
-rw-r--r--src/ng/http.js23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/ng/http.js b/src/ng/http.js
index 2b9c7475..d81b7912 100644
--- a/src/ng/http.js
+++ b/src/ng/http.js
@@ -535,7 +535,9 @@ function $HttpProvider() {
* - **params** – `{Object.<string|Object>}` – Map of strings or objects which will be turned to
* `?key1=value1&key2=value2` after the url. If the value is not a string, it will be JSONified.
* - **data** – `{string|Object}` – Data to be sent as the request message data.
- * - **headers** – `{Object}` – Map of strings representing HTTP headers to send to the server.
+ * - **headers** – `{Object}` – Map of strings or functions which return strings representing
+ * HTTP headers to send to the server. If the return value of a function is null, the header will
+ * not be sent.
* - **xsrfHeaderName** – `{string}` – Name of HTTP header to populate with the XSRF token.
* - **xsrfCookieName** – `{string}` – Name of cookie containing the XSRF token.
* - **transformRequest** – `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
@@ -736,6 +738,10 @@ function $HttpProvider() {
defHeaders = extend({}, defHeaders.common, defHeaders[lowercase(config.method)]);
+ // execute if header value is function
+ execHeaders(defHeaders);
+ execHeaders(reqHeaders);
+
// using for-in instead of forEach to avoid unecessary iteration after header has been found
defaultHeadersIteration:
for (defHeaderName in defHeaders) {
@@ -751,6 +757,21 @@ function $HttpProvider() {
}
return reqHeaders;
+
+ function execHeaders(headers) {
+ var headerContent;
+
+ forEach(headers, function(headerFn, header) {
+ if (isFunction(headerFn)) {
+ headerContent = headerFn();
+ if (headerContent != null) {
+ headers[header] = headerContent;
+ } else {
+ delete headers[header];
+ }
+ }
+ });
+ }
}
}