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

Resource response transformation executed before HTTP interceptor #7594

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

Resource response transformation executed before HTTP interceptor #7594

ce07c3 opened this issue May 25, 2014 · 5 comments

Comments

@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.

@scriby
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
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
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
Copy link
Member

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.

@dchacke
Copy link

dchacke 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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants