2

i'm doing a http GET request to a url which has a xml file and i need to get the response header 'Content-Lenght' ? Is there any way to get it? i must validate the size of the file before been abel to download it.

Here's my code

$http({
     method : "GET",
     url : "http://url/file.xml"
  }).then(function mySucces(data) {

    console.log(response);
    $scope.content = response.data;

  }, function myError(error) {

    console.log(error);
    $scope.content = error.statusText;
  });
Franco Manzur
  • 373
  • 1
  • 7
  • 19

1 Answers1

1

From Angular Docs, https://docs.angularjs.org/api/ng/service/$http

response callback gets

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

You can get response headers by calling headers function with header name, in this case headers('Content-Type')

$http({
  method: "GET",
  url: "http://b31fe90a.ngrok.io/xml/XML-Meli.xml"
}).then(function mySucces(response) {

  console.log(response);
  console.log(response.headers('content-type'); // it can be `Content-Type` not sure. but can be any header key.
  $scope.content = response.data;

}, function myError(error) {

  console.log(error);
  $scope.content = error.statusText;
});
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
  • it says that headers it's not a function – Franco Manzur Jul 04 '16 at 18:31
  • This doesn't work for me with the 'Content-Length' header. Your example shows 'Content-Type', but when asking for 'Content-Length' it just returns `null`. I still haven't found any way to get this to work with 'Content-Length'. – jkjustjoshing Aug 17 '16 at 15:43
  • @jkjustjoshing Did you try this. http://stackoverflow.com/questions/38190998/angularjs-get-headers-in-response – Zohaib Ijaz Aug 17 '16 at 16:47
  • Careful, CORS might not allow you to access that header even when doing it right : http://stackoverflow.com/a/24073356/1478467 – Sherbrow Nov 07 '16 at 11:25