Conditional transformRequest in $resource.$save

37 Views Asked by At

I use $resource extensively. I would like to be able to set the transformRequest property on $save on some routes/resources but not on others. How can I do that?

In the $resource(url, [paramDefaults], [actions], options); definition, you can only set the transformRequest on the custom actions, not the $save method.

In the $httpProvider.defaults.transformRequest you can't determine which route/method you are on as you don't have access to the url/path, only the params data.

1

There are 1 best solutions below

0
Sean On

The best I can come up with:

$httpProvider.interceptors.push(function () {
    return {
        "request": function (config) {
            if (config.method === "POST" && new RegExp("{myregexhere}").test(config.url)) {
                // keep only the default
                config.transformRequest.length = 1;
            }
            return config;
        }
    };
});