0

I'm trying to process the response headers but not sure how to query for them. Here's the code snippet. I commented out a couple of lines when I attempted to read the headers and put some comments on what I noticed.

$http({
method: 'POST',
url: URL,
data: $.param(data),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function (data,status,headers) {
//Need to process the header to understand the server response
//console.log(headers()); //This returns null
//console.log(headers('custom-myapp-text');// Obvisouls returns null as the headers() returns null
deferred.resolve();
});
return deferred.promise;
};

As per their documentation, the 'headers' returns a function?? Not sure how to query for header values based on this.

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.
user6123723
  • 10,546
  • 18
  • 67
  • 109
  • Possible duplicate of http://stackoverflow.com/questions/19454477/angularjs-accessing-http-headers – Kevin Friedheim Oct 14 '15 at 17:22
  • Calling `headers()` without any arguments will return an object of all the headers available to you. You can then get individual headers by key. What is the status code? Perhaps your request is returning an error? – SirTophamHatt Oct 14 '15 at 17:25
  • @KevinFriedheim Nope. That solution doesn't work. As you can see in the description, the headers() returns null. The link you put in points to a solution where headers() is added to the return array. Which doesn't appear to work. My Question is why and how to read Angular's documentation on this headers- {function([headerName])} – user6123723 Oct 14 '15 at 17:26
  • " //console.log(headers('error-text'); // Obviously returns null as the headers() returns null" If you're query for error response header, you can't do that in the success callback. Try chaining an error callback. – SirTophamHatt Oct 14 '15 at 17:27
  • @SirTophamHatt It's actually an application header. The call is actually successful with a 200. So it's only available in the Success callback. – user6123723 Oct 14 '15 at 17:29
  • @uhsarp `headers()` is a getter function - if you don't specify something for it to retrieve, it won't retrieve anything. In your example `headers('error-text')` wiill return null becuase, well, you're in the `.success` function. – Kevin Friedheim Oct 14 '15 at 17:55
  • @KevinFriedheim It's actually an application header and is returned by the server on a successful call. The call is actually successful with a 200. So it's only available in the Success callback. But I see how the name is confusing. So I updated the header name to avoid this confusion. – user6123723 Oct 14 '15 at 19:25

1 Answers1

1

I just tried it with a valid header and it was fine:

console.log(headers('Content-Length'));

This console logged 2 in my example. It will allow you access to any of the valid response headers.

Mathew Berg
  • 28,625
  • 11
  • 69
  • 90