This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Allow transformRequest to modify headers #12095
Closed
Description
In AngularJS 1.4.0-beta5 the possibility to modify request headers from transformRequest
functions has been removed. This is stated in the changelog. I found this a very useful feature to modify the Content-Type
of the request being made.
For example:
$http.post('', {
foo: 'bar'
}, {
transformRequest: function(data, headers) {
headers()['Content-Type'] = 'application/x-www-form-urlencoded';
var parsed = [];
angular.forEach(data, function(value, key) {
parsed.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
});
return parsed.join('&');
}
});
I often use a similar approach to make form-data
requests, which requires the Content-Type
to be set to undefined
.
In practice I have created an xWwwFormUrlencoded
and formData
factory, because I use them more often.
So the code would look like this:
$http.post('', {
foo: 'bar'
}, {
transformRequest: xWwwFormUrlencoded
});
It feels like a huge drawback having to to explicitly specify the headers whenever making such a transformed request.
The following plunkr demonstrates this issue. The test breaks when updating AngularJS to 1.4.0
.