Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 73ab107

Browse files
docs($http): clarify side effects of transformRequest functions
Closes #11438 Closes #11503
1 parent 9dfa949 commit 73ab107

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

CHANGELOG.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,34 @@ end of the container containing the ngMessages directive).
249249
</div>
250250
```
251251

252+
- **$http:** due to [5da1256](https://github.com/angular/angular.js/commit/5da1256fc2812d5b28fb0af0de81256054856369),
253+
254+
`transformRequest` functions can no longer modify request headers.
255+
256+
Before this commit `transformRequest` could modify request headers, ex.:
257+
258+
```javascript
259+
function requestTransform(data, headers) {
260+
headers = angular.extend(headers(), {
261+
'X-MY_HEADER': 'abcd'
262+
});
263+
}
264+
return angular.toJson(data);
265+
}
266+
```
267+
268+
This behavior was unintended and undocumented, so the change should affect very few applications. If one
269+
needs to dynamically add / remove headers it should be done in a header function, for example:
270+
271+
```javascript
272+
$http.get(url, {
273+
headers: {
274+
'X-MY_HEADER': function(config) {
275+
return 'abcd'; //you've got access to a request config object to specify header value dynamically
276+
}
277+
}
278+
})
279+
```
252280

253281
<a name="1.3.14"></a>
254282
# 1.3.14 instantaneous-browserification (2015-02-24)

test/ng/httpSpec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,23 @@ describe('$http', function() {
11281128
expect(callback).toHaveBeenCalledOnce();
11291129
});
11301130

1131+
it('should not allow modifications to headers in a transform functions', function() {
1132+
var config = {
1133+
headers: {'Accept': 'bar'},
1134+
transformRequest: function(data, headers) {
1135+
angular.extend(headers(), {
1136+
'Accept': 'foo'
1137+
});
1138+
}
1139+
};
1140+
1141+
$httpBackend.expect('GET', '/url', undefined, {Accept: 'bar'}).respond(200);
1142+
$http.get('/url', config).success(callback);
1143+
$httpBackend.flush();
1144+
1145+
expect(callback).toHaveBeenCalledOnce();
1146+
});
1147+
11311148
it('should pipeline more functions', function() {
11321149
function first(d, h) {return d + '-first' + ':' + h('h1');}
11331150
function second(d) {return uppercase(d);}

0 commit comments

Comments
 (0)