Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Response interceptor on resource does not allow me to change data #4407

Closed
honzajde opened this issue Oct 14, 2013 · 10 comments
Closed

Response interceptor on resource does not allow me to change data #4407

honzajde opened this issue Oct 14, 2013 · 10 comments

Comments

@honzajde
Copy link

query: {
    method: 'GET',
        interceptor: {
        response: function(response) {
            console.debug('Tags: ', response.data.tags);
            response.data = response.data.tags;
            return response;
        }
    }
}

After resource is resolved it should be tags but its data.

@honzajde
Copy link
Author

I guess, transformResponse should be used for modifying data - but this is not documented.

@honzajde
Copy link
Author

angular1.2rc2

@honzajde
Copy link
Author

Correct way to modify response is obviously:

                        isArray:true,
                        transformResponse: function(data, headersGetter){
                            data = angular.fromJson(data);
                            return data.tags;
                        }

@petebacondarwin
Copy link
Contributor

It is documented here: http://docs.angularjs.org/api/ng.$http under the section entitled "Transforming Requests and Responses"
I am surprised that your interceptor method did not work, though. What exactly is the object you get passed to your then() function?

@honzajde
Copy link
Author

The documentation link points to $http service, while this is using $resource.

@pdeszynski
Copy link

@honzajde I knot this post is really old, but I was also looking for a solution and didn't found one on the internet.

In the function you provided, you should be modifying response.resource not the response.data if you want to modify the output (tested in Angular 1.3)

@webskin
Copy link

webskin commented Mar 12, 2014

@piteer1 I have the same problem with interceptor. Your solution works but response.resource cannot be replaced. We have to modified the values inside the reference … less handy than transformResponse but with transformResponse i cannot get the status code …

@markwpearce
Copy link

I think it's weird that passing an interceptor into an action on a $resource doesn't let the interceptor intercept requests, only responses.

For example:

var Thing = $resource('/thing/:id',
      {thingId:'@id'},
     {
         foo: {method:'POST', interceptor: myInterceptor}
     }
);

myInterceptor will only intercept the response of Thing.foo(), NOT the request...

@GabrielDelepine
Copy link

I know this issue is closed, but for anyone who search :

To modify the data in a resource interceptor, you need to modify response.data and response.resource. Example of the weird behaviour :

live example with AngularJS 1.2.27 : http://jsfiddle.net/605twneu/2/

Should I open an issue ? What do you think ?

@anaumov
Copy link

anaumov commented Sep 9, 2015

If anyone still search for answer look at this issue #11409, there is detailed discussion about interceptors and why $resource interceptors different from $http.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants