Franco Manzur Franco Manzur - 2 months ago 20
AngularJS Question

angularjs - how i can get the content-length header

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;
});

Answer

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;
});
Comments