Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pushing or Unshifting into the transformRequest array of $http (non-global)

See the accepted answer to here for a pretty good explanation on the transFormRequest function/array.

In the answer's last example:

var transform = function(data){
    return $.param(data);
}

$http.post("/foo/bar", requestData, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
    transformRequest: transform
}).success(function(responseData) {
    //do stuff with response
});

However, the problem with this is that transformRequest: transform overwrites the array of functions that Angular has pre-built.

From the Angular docs:

To globally augment or override the default transforms, modify the $httpProvider.defaults.transformRequest and $httpProvider.defaults.transformResponse properties. These properties are by default an array of transform functions, which allows you to push or unshift a new transformation function into the transformation chain. You can also decide to completely override any default transformations by assigning your transformation functions to these properties directly without the array wrapper. These defaults are again available on the $http factory at run-time, which may be useful if you have run-time services you wish to be involved in your transformations.

Similarly, to locally override the request/response transforms, augment the transformRequest and/or transformResponse properties of the configuration object passed into $http.

If I wanted to apply my transform function globally, I would do

$httpProvider.defaults.transformRequest.unshift(myFunction)

or

$httpProvider.defaults.transformRequest.push(myFunction)

My question
Instead of erasing the entire array of transform request functions, how to you push another transform function to a call, not globally?

like image 807
NicolasMoise Avatar asked Feb 14 '14 19:02

NicolasMoise


People also ask

How do you modify the $HTTP request default Behaviour?

To add or overwrite these defaults, simply add or remove a property from these configuration objects. To add headers for an HTTP method other than POST or PUT, simply add a new object with the lowercased HTTP method name as the key, e.g. $httpProvider. defaults.

What is $HTTP in AngularJS?

$http is an AngularJS service for reading data from remote servers.

How can I POST data as form data instead of a request payload in angular?

How can I make AngularJS submit xsrf as form data instead of a request payload? var url = 'http://somewhere.com/'; var xsrf = {fkey: 'xsrf key'}; $http({ method: 'POST', url: url, data: xsrf }). success(function () {}); $. ajax({ type: 'POST', url: url, data: xsrf, dataType: 'json', success: function() {} });

How do we pass data and get data using http in angular?

Use the HttpClient.get() method to fetch data from a server. The asynchronous method sends an HTTP request, and returns an Observable that emits the requested data when the response is received. The return type varies based on the observe and responseType values that you pass to the call.


2 Answers

I found an easy solution using the .concat method

{
   transformRequest: [function(req){...}].concat($http.defaults.transformRequest)
}

or alternatively, if you want your custom transformation to take place after angular's default transformations.

{
   transformRequest: $http.defaults.transformRequest.concat([function(req){...}])
}
like image 164
NicolasMoise Avatar answered Oct 18 '22 13:10

NicolasMoise


Angular, in the documentation makes this suggestion:

$httpProvider.defaults.transformRequest = appendTransform($httpProvider.defaults.transformRequest, function(data) {
    //do whatever you want
    return data;
});

function appendTransform(defaults, transform) {

    // We can't guarantee that the default transformation is an array
    defaults = angular.isArray(defaults) ? defaults : [defaults];

    // Append the new transformation to the defaults
    return defaults.concat(transform);
}
like image 3
jfcorugedo Avatar answered Oct 18 '22 13:10

jfcorugedo