19

I basically call get requests like so:

var resource = $resource('/api/v1/categories/:id')

resource.get({id: 1}).$promise.then(function(data){
  console.log(data)
})

This works fine.. but how do I get the response headers?

3 Answers 3

42

You could use the transformResponse action defined here this would allow you add the headers

$resource('/', {}, {
    get: {
        method: 'GET',
        transformResponse: function(data, headers){
            response = {}
            response.data = data;
            response.headers = headers();
            return response;
        }
    }

See a working example here JSFiddle

Sign up to request clarification or add additional context in comments.

3 Comments

doesn't work with newer versions. Function that transforms response should return deserialized data now
Simply access the headersGetter function that gets passed as a second parameter after your data. See the answer below or here: docs.angularjs.org/api/ngResource/service/$resource
Access a specific header like this: headers('Server')
18

@Martin's answer works for same domain requests. So I would like to add to his answer that if you are using cross domain requests, you will have to add another header with Access-Control-Expose-Headers: X-Blah, X-Bla along with the Access-Control-Allow-Origin:* header.

where X-Blah and X-Bla are custom headers.

Also you do not need to use transform request to get the headers. You may use this code:

var resource = $resource('/api/v1/categories/:id')

resource.get({id: 1}, function(data, headersFun){
  console.log(data, headersFun());
})

See this fiddle. Hope this helps !!!

4 Comments

I think this is more convenient than the current accepted answer (At least in 2016). Angular automatically gives you header values with the 2nd parameter (Do not forget to use paranthesis as this is a function, not a direct object).
It's less important about the cross-domain and more relevant about the headers function being a second param in the then function - I almost missed this because of your cross-domain info!
There is a subtle but hugely important difference between what you wrote here and in the fiddle. The fiddle is using the get call with callbacks for success and error, and in that case the headersFun exists. But handling the response via $promise.then as you wrote above has no such second parameter - headersFun is undefined.
@Danny improved the answer as per your suggestion.
6

Old question, but i think it's worth mentioning for the future reference. There is 'official' solution for this in angular documentation:

It's worth noting that the success callback for get, query and other methods gets passed in the response that came from the server as well as $http header getter function, so one could rewrite the above example and get access to http headers as:

var User = $resource('/user/:userId', {userId:'@id'});

var users = User.query({}, function(users, responseHeaders){
  // ...
  console.log(responseHeaders('x-app-pagination-current-page'));
});

(code from docs slightly updated for clarity)

For CORS requests, exposing headers as mentioned in other answers is required.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.