0

I have the following $resouce service:

$scope.scenesResource = $resource(scenesUrl + ':lang/:id', {
                                      lang: '@lang',
                                      id: '@id'
                                  }, {
                                      create: {
                                           method: 'POST',
                                           headers: {
                                              Authorization: localStorage.tokenType + ' ' + localStorage.accessToken 
                                           },
                                           transformResponse: function (data, headers) {
                                                //This is never getting called
                                                console.log(data); 
                                                console.log(headers);
                                           }
                                      }
                                  };

Despite any security issues, such as reading the access token from local storage, or never expiring it, the create method works fine.

I mean, it makes a request with the correct Authorization header, and gets a 201 Created response from the server.

The problem is that, the server, instead of responding with the new resource's id in the response body or payload, it returns the Location header:

Location: /scenes/en-us/ODc1ZmYwNzUtZDA5MS00ZWJkLTgyODYtZWUzMGMyMDdhMmYx

I have a promise function on the create method:

newResource.$create().then(function (newScene) {
    //Handle scene
});

As you can see, I need newScene to be filled with all the information that the server responded so I can handle it properly. But this information is in the Location header.

I am unable to read this header using the transformResponse method above in the example, because the method is never getting called.

How can I read my Location header using $resource?

Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
  • try to read this post if you haven't read yet http://stackoverflow.com/a/17903447/3316654 – wilver Aug 20 '15 at 10:00
  • @wilver That works if you're using `$httpProvider`. but I am using the `$resource` service. – Matias Cicero Aug 20 '15 at 11:53
  • $resource depends on $http and $http make the transformResponse at its own level. You can also see: http://stackoverflow.com/questions/21515481/angularjs-transformresponse or http://stackoverflow.com/questions/15455387/resource-transformresponse-not-working. Which version of Angular.js are you using? – wilver Aug 20 '15 at 13:19
  • I have read the questions but can't solve my problem still. I know about `transformResponse` being executed in an `$http` level, and in fact I am using it in my question. The problem is it's not executing. I have the latest AngularJS version. – Matias Cicero Aug 20 '15 at 13:21
  • Can you return a 200 or 202 http status code from server call? If so, you get the same behaviour? try to look at this: http://www.trajano.net/2013/05/201-created-with-angular-resource/ – wilver Aug 20 '15 at 14:06
  • have you resolved the problem? – wilver Aug 23 '15 at 08:26
  • @wilver Yes, thank you very much for your support. I removed the `transformResponse` method from the `$create` definition and instead passed in a callback function to the `$create` call on the resource. This callback function is given the headers I need. For example: `newResource.$create(function (data, headers) { var location = headers('Location'); })` – Matias Cicero Aug 23 '15 at 18:37

0 Answers0