Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read response headers with $resource?

I'm using $resource to get data from my RESTful service and I need to read response headers to get 'X-Page' and 'X-Total-Pages' value for pagination.

Example:

Access-Control-Max-Age:1728000
Cache-Control:max-age=0, private, must-revalidate
Connection:Keep-Alive
Content-Length:2637
Content-Type:application/json
Date:Thu, 10 Apr 2014 16:53:01 GMT
Server:WEBrick/1.3.1 (Ruby/2.1.1/2014-02-24)
Vary:Origin
X-Page:1
X-Per-Page:10
X-Total:17
X-Total-Pages:2

But I couldn't get full headers from server.

This is returned headers: enter image description here

This is the headers from server: enter image description here

This is my code:

.factory('TestAPI', ['$resource',
        function ($resource) {
            return $resource("http://ip.jsontest.com/?callback=showIP", {}, {
                query: {
                    method: 'GET'
                }
            });
        }])

TestAPI.query({}, function (value, responseHeaders) {
                console.log(responseHeaders());
            }, function (response) {
                console.log(response);
 });
like image 482
HNQ Avatar asked Apr 10 '14 22:04

HNQ


People also ask

How do I view response headers?

View headers with browser development toolsWithin the page, right-click (for PC users) or command-click (Mac users) to view options, and then click Inspect. The Inspect feature will display the troubleshooting console, which will enable all of the available tools in a panel on the side of the window.

How do I read response headers in node JS?

In node, the headers are accessed by using lowercased names, so using response. headers['content-encoding'] is correct. Your code snippet currently works for me and displays 'server encoded the data as: gzip.

How do I view response headers in react?

Instead of trying to access the name of the header right on the “headers” object, you have to use the get function to pull the header data. This can be used in a variety of ways, for example making requests to any number of API's in frontend react applications.

How do I get response headers from Fetch?

We then fetch this request using fetch() , extract a blob from the response using Response. blob , create an object URL out of it using URL. createObjectURL , and display this in an <img> . Note that at the top of the fetch() block, we log the response headers to the console.


1 Answers

In your response headers you have to add the following header:

Access-Control-Expose-Headers: X-Total-Pages, X-Page

With this, the browser is capable to expose your customs headers an read it angular.

like image 131
nancoder Avatar answered Sep 29 '22 06:09

nancoder