6

I am adding a pair of actions to an AngularJS resource, but when I invoke the action, my transformRequest function is not getting called:

    var _resource = $resource('api/NewItem/:id',
    { id: '@id' },
    {
        create: {
            method: 'POST',
            transformRequest: function (data, headersGetter) {
                var result = JSON.stringify(data.productIntro);
                return result;
            }
        },
        update: {
            method: 'PUT',
            transformRequest: function (data, headersGetter) {
                var result = JSON.stringify(data.productIntro);
                return result;
            }
        }
    });

If I add the function globally on the app, it works:

var newItemApp = angular.module('newItemApp', ['ngResource'])
.config(function ($httpProvider) {
    $httpProvider.defaults.transformRequest = function(data)
    {
        if (data === undefined) {
            return data;
        }
        var result = JSON.stringify(data.productIntro);
        return result;
    };
});

What I need to do is remove the root element from any POST or PUT action because the default model binding in Web Api does not bind a json object when that object has a named root.

1 Answer 1

5

transformRequest is supported since AngularJS 1.1.2. If you use early version, you need to add it to $httpProvider.

Sign up to request clarification or add additional context in comments.

2 Comments

Yup - I thought we had the latest version in our build, but I was wrong. Thanks!
I had the same issue - I was calling .$save on my resource but was setting transformRequest on 'create'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.