-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Allow transformRequest to modify headers #12095
Comments
Hmm. I can see how this in inconvenient. There are basically two ways
However, if you have a factory for the transformRequest, couldn't you simply do:
|
Yeh, I can see how this is inconvenient and while interceptors work they add overhead of the async processing. having said this I really dislike APIs where input arguments are getting modified.... Maybe we should make it possible to return both transformed data and headers from the transform request function? |
The headers property could also accept a function that gets passed the current config + default headers and is expected to return a headers object. |
Actually using an interceptor sounds like a perfect solution in my case. I could just filter out requests which specify an If the function would be able to return both the transformed data and headers, I imagine this would return an array consisting of If the headers would accept a function for transforming the headers, this means that a modifications to body according to the content type would happen in the headers function as well. Another option could be that transformRequest accepts either a function or an object. If a function is passed, it behaves the way it normally does. $http.post('', {
foo: 'bar'
}, {
transformRequest: {
body: function(data, headersGetter) {
return myTransformFn(data);
},
headers: function(data, headersGetter) {
var headers = headersGetter();
headers['Content-Type'] = 'x-www-form-urlencoded';
return headers;
}
}
}); |
There are many ways to solve it cleanly, ex.: returning an instance of a specific class. So I don't think we are blocked from applying this solution, implementation-wise. |
I was using this feature too... It's really important to me the ability to change headers in the transform request, instead of in every single function... please bring this feature back if possible... |
Agreed, we now have to find a work around while updating. |
+1 👍 |
Any workaround on this issue? |
@wmfairuz yes, interceptors |
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 theContent-Type
of the request being made.For example:
I often use a similar approach to make
form-data
requests, which requires theContent-Type
to be set toundefined
.In practice I have created an
xWwwFormUrlencoded
andformData
factory, because I use them more often.So the code would look like this:
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
.The text was updated successfully, but these errors were encountered: