4

do you know how i can access the response header from a get request ?

I have a service function which return a promise. My Controller resolve the promise and in .then function i need the content type from the response header.

I have try to use the "headers" parameter, which i display with console.log(headers()), but the error "headers() is not a function" shown in my console.

My Service :

.factory('GetResultFile',
['$http', '$q',
function ($http, $q) {

    var service = {};
    service.getResult = function(id, rid) {

        var deferred = $q.defer();

        $http
        .get('http://localhost:9999/v1/jmeter/' + id + '/results/' + rid, {cache: false})
        .then(function(data, status, headers, config) {
            if(data.status == 200) {
                console.log(data.status);
                deferred.resolve(data);
            }
            else {
                deferred.reject(data);
            }
        });
        return deferred.promise;                
    }
    return service;

}]);

controller:

$scope.getResult = function(rid) {
        console.log($scope.id);
        GetResultFile.getResult($scope.id, rid)
        .then(function(data, headers) {
            //console.log(headers.Content-type);
            console.log(headers());
            console.log(data);
            console.log("Download succeed");
            console.log(data.status);

            var file = new Blob([data.data], {type: 'text/plain;charset=utf-8'});
            FileSaver.saveAs(file, 'test.txt');

        }, function (data) {
            console.log("Download ERROR!");
            console.log(data.status);
        })
    };              

}])

Kai
  • 79
  • 1
  • 3
  • 12

1 Answers1

8

Without some more Info I can only think about the Standard things like

    this.$http.get('/your/Route')
      .then(response => {
        console.log(response) // Full information
        console.log(response.data) // holds your Data
        console.log(response.config) // holds more Specific information like the Url and more
        console.log(response.headers());// Specific headers Information
        console.log(response.headers(["content-type"]));//gets the Content-Type of Header

});

Generally to Angular Service and 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.

https://docs.angularjs.org/api/ng/service/$http

AkAk47
  • 291
  • 4
  • 18
  • hi, thank you for the answer. One question : how can i check the content type from the response header ? console.log(headers([content-type]) ? – Kai Jan 10 '17 at 14:23
  • @Kai Yeah just do `console.log(response.headers(["content-type"]));`. Will Edit my Answer with that Solution – AkAk47 Jan 10 '17 at 14:38
  • thank you, i have try it with console.log(response.header('content-type')) works fine ! – Kai Jan 10 '17 at 14:49