33

My server returns this kind of header: Content-Range:0-10/0:

enter image description here

I tried to read this header in angular with no luck:

var promise = $http.get(url, {
    params: query
}).then(function(response) {
  console.log(response.headers());
  return response.data;
});

which just prints

Object {content-type: "application/json; charset=utf-8"}

Any ideas how to access the content range header?

Community
  • 1
  • 1
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601

7 Answers7

40

Use the headers variable in success and error callbacks

From documentation.

$http.get('/someUrl').
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
  })
  .error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

If you are on the same domain, you should be able to retrieve the response headers back. If cross-domain, you will need to add Access-Control-Expose-Headers header on the server.

Access-Control-Expose-Headers: content-type, cache, ...
Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
  • This still does not give me the response headers, just the request headers. – DarkLeafyGreen Mar 02 '15 at 08:48
  • I have cross-domain enabled already. The screenshot in my question shows that the content-range header is inside the response in the chrome inspector. So the server is not the problem. – DarkLeafyGreen Mar 02 '15 at 08:57
  • Yes, CORS returns certain headers by default. And you need to tell it which other headers are safe to be exposed via the api. [See this](http://www.w3.org/TR/cors/#access-control-expose-headers-response-header). – Muhammad Reda Mar 30 '15 at 08:57
  • 7
    The success call seems to be deprecated and shouldn't be used. Check docs: https://docs.angularjs.org/api/ng/service/$http – Vitalij Jun 21 '16 at 16:19
  • 2
    THIS DOES NOT WORK ANYMORE! see: https://stackoverflow.com/questions/41169385/http-get-success-is-not-a-function – Hobbamok Sep 27 '18 at 11:06
  • will it work to fetch set-cookie(with httponly flag) from response header? – jay rangras Jun 15 '21 at 11:12
34

Why not simply try this:

var promise = $http.get(url, {
    params: query
}).then(function(response) {
  console.log('Content-Range: ' + response.headers('Content-Range'));
  return response.data;
});

Especially if you want to return the promise so it could be a part of a promises chain.

Eugene Retunsky
  • 13,009
  • 4
  • 52
  • 55
  • 2
    Giving a (+1) because `response` has a `headers()` method which takes a `name` parameter. This is my issue though; I cannot retrieve `Authorization` header [or any others] via `response.headers('Authorization')` even when I can see `response.config.headers['Authorization']` in plain sight!!! What is the world is this function for then??? – Cody Jun 10 '16 at 04:12
  • Is it a cross-domain request? – Eugene Retunsky Jun 13 '16 at 21:02
  • You should probably not work with the A‌​uthorization in a single request like this. Use an interceptor for Authorization stuff – Jens Alenius Aug 16 '16 at 08:20
  • @EugeneRetunsky : I have cross domain request. I can see the authorization header in response, but now able to fetch it. Anyway I can do that as API is returning token for successful authentication. – Bhargav Bhanderi Mar 23 '17 at 09:45
  • i'm getting "headers is not a function" – sports Jun 20 '18 at 21:07
  • 2
    `Authorization` is a common _request_ header. I have never seen it as a response header. Are you sure it's actually in your response? The `response.config.headers` may be referring to the request, not the response. – Brandon Nov 04 '19 at 17:52
12

Updated based on Muhammad's answer...

$http.get('/someUrl').
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
    console.log(headers()['Content-Range']);
  })
  .error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });
medoix
  • 1,189
  • 2
  • 16
  • 36
7

Additionally to Eugene Retunsky's answer, quoting from $http documentation regarding the response:

The response object has these properties:

  • data{string|Object} – The response body transformed with the transform functions.

  • status{number} – HTTP status code of the response.

  • headers{function([headerName])} – Header getter function.

  • config{Object} – The configuration object that was used to generate the request.

  • statusText{string} – HTTP status text of the response.

Please note that the argument callback order for $resource (v1.6) is not the same as above:

Success callback is called with (value (Object|Array), responseHeaders (Function), status (number), statusText (string)) arguments, where the value is the populated resource instance or collection object. The error callback is called with (httpResponse) argument.

Community
  • 1
  • 1
Wtower
  • 18,848
  • 11
  • 103
  • 80
2
response.headers();

will give you all the headers (defaulat & customs). worked for me !!

enter image description here

enter image description here

Note . I tested on the same domain only. We may need to add Access-Control-Expose-Headers header on the server for cross domain.

Pradeep Singh
  • 1,094
  • 1
  • 9
  • 24
1

The response headers in case of cors remain hidden. You need to add in response headers to direct the Angular to expose headers to javascript.

// From server response headers :
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, 
Content-Type, Accept, Authorization, X-Custom-header");
header("Access-Control-Expose-Headers: X-Custom-header");
header("X-Custom-header: $some data");

var data = res.headers.get('X-Custom-header');

Source : https://github.com/angular/angular/issues/5237

1

According the MDN custom headers are not exposed by default. The server admin need to expose them using "Access-Control-Expose-Headers" in the same fashion they deal with "access-control-allow-origin"

See this MDN link for confirmation [https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers]