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

docs($http): clarify side effects of transformRequest functions #11503

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,34 @@ end of the container containing the ngMessages directive).
</div>
```

- **$http:** due to [5da1256](https://github.com/angular/angular.js/commit/5da1256fc2812d5b28fb0af0de81256054856369),

`transformRequest` functions can no longer modify request headers.

Before this commit `transformRequest` could modify request headers, ex.:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super pedantic, but I prefer "for example" to "ex."


```javascript
function requestTransform(data, headers) {
headers = angular.extend(headers(), {
'X-MY_HEADER': 'abcd'
});
}
return angular.toJson(data);
}
```

This behavior was unintended and undocumented, so the change should affect very few applications. If one
needs to dynamically add / remove headers it should be done in a header function, for example:

```javascript
$http.get(url, {
headers: {
'X-MY_HEADER': function(config) {
return 'abcd'; //you've got access to a request config object to specify header value dynamically
}
}
})
```

<a name="1.3.14"></a>
# 1.3.14 instantaneous-browserification (2015-02-24)
Expand Down
17 changes: 17 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,23 @@ describe('$http', function() {
expect(callback).toHaveBeenCalledOnce();
});

it('should not allow modifications to headers in a transform functions', function() {
var config = {
headers: {'Accept': 'bar'},
transformRequest: function(data, headers) {
angular.extend(headers(), {
'Accept': 'foo'
});
}
};

$httpBackend.expect('GET', '/url', undefined, {Accept: 'bar'}).respond(200);
$http.get('/url', config).success(callback);
$httpBackend.flush();

expect(callback).toHaveBeenCalledOnce();
});

it('should pipeline more functions', function() {
function first(d, h) {return d + '-first' + ':' + h('h1');}
function second(d) {return uppercase(d);}
Expand Down