@@ -535,7 +535,9 @@ function $HttpProvider() {
535
535
* - **params** – `{Object.<string|Object>}` – Map of strings or objects which will be turned to
536
536
* `?key1=value1&key2=value2` after the url. If the value is not a string, it will be JSONified.
537
537
* - **data** – `{string|Object}` – Data to be sent as the request message data.
538
- * - **headers** – `{Object}` – Map of strings representing HTTP headers to send to the server.
538
+ * - **headers** – `{Object}` – Map of strings or functions which return strings representing
539
+ * HTTP headers to send to the server. If the return value of a function is null, the header will
540
+ * not be sent.
539
541
* - **xsrfHeaderName** – `{string}` – Name of HTTP header to populate with the XSRF token.
540
542
* - **xsrfCookieName** – `{string}` – Name of cookie containing the XSRF token.
541
543
* - **transformRequest** – `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
@@ -736,6 +738,10 @@ function $HttpProvider() {
736
738
737
739
defHeaders = extend ( { } , defHeaders . common , defHeaders [ lowercase ( config . method ) ] ) ;
738
740
741
+ // execute if header value is function
742
+ execHeaders ( defHeaders ) ;
743
+ execHeaders ( reqHeaders ) ;
744
+
739
745
// using for-in instead of forEach to avoid unecessary iteration after header has been found
740
746
defaultHeadersIteration:
741
747
for ( defHeaderName in defHeaders ) {
@@ -751,6 +757,21 @@ function $HttpProvider() {
751
757
}
752
758
753
759
return reqHeaders ;
760
+
761
+ function execHeaders ( headers ) {
762
+ var headerContent ;
763
+
764
+ forEach ( headers , function ( headerFn , header ) {
765
+ if ( isFunction ( headerFn ) ) {
766
+ headerContent = headerFn ( ) ;
767
+ if ( headerContent != null ) {
768
+ headers [ header ] = headerContent ;
769
+ } else {
770
+ delete headers [ header ] ;
771
+ }
772
+ }
773
+ } ) ;
774
+ }
754
775
}
755
776
}
756
777
0 commit comments