New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource response transformation executed before HTTP interceptor #7594

Closed
ce07c3 opened this Issue May 25, 2014 · 5 comments

Comments

Projects
None yet
9 participants
@ce07c3
Copy link

ce07c3 commented May 25, 2014

Shouldn't it be the other way around? I would not want to transform a 401-response.

@Narretz Narretz added this to the Ice Box milestone Jun 25, 2014

@scriby

This comment has been minimized.

Copy link

scriby commented Jul 8, 2014

I ran into this today. When specifying an interceptor for a $resource action, the response interceptor runs after the response.resource object has been populated. I need to transform the data before the resources are constructed.

@dtheodor

This comment has been minimized.

Copy link

dtheodor commented Sep 3, 2014

If I understand your use-case correctly, if you want to modify the data before they are turned into the Resource object you should use the transformResponse option. The interceptor is provided to modify the Resource object as it would be returned if there was no interceptor.

@jonlil

This comment has been minimized.

Copy link

jonlil commented Nov 24, 2014

I also ran into this today, running angular & angular-resource 1.3.3. Added a method to $http.defaults.transformResponses chain that catches /Unauthorized/.test(arguments[0])

Any progress / updates?
Cheers

@pkozlowski-opensource

This comment has been minimized.

Copy link
Member

pkozlowski-opensource commented Dec 14, 2014

A fix landed in master (1b74097) that makes it possible to inspect response status code in the response data transformer function and take an appropriate action based on this status. Hopefully this is enough to address this issue as now people can avoid response data transformation for failed requests.

Closing this for now, please open a separate issue if anything is missing after this change.

@weltschmerz1

This comment has been minimized.

Copy link

weltschmerz1 commented Jan 26, 2016

To anyone coming here from Google like I did:

I also found it to be confusing that response interceptors run after the transformResponse method. I followed @jonlil's advice and also added a method to $http.defaults.transformResponse. Here is an example from the documentation on how to do that.

So, if you need to basically have a response interceptor that runs before the transformResponse method, this should do it:

'use strict';

angular.module('app')
  .run(function ($http) {
    $http.defaults.transformResponse.push(function (data, headers) {
      // do stuff here before the response transformation

      // Be sure to return `data` so that the next function in the queue can use it.
      // Your services won't load otherwise!
      return data;
    });
  });

If your services or http calls don't have their own response transformer, you're good now.

If your services do have their own transformResponse method, they will actually override all default transformers (I found this out after a long read of the documentation), and the above code will not run.

To circumvent this, you can follow this example in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment