Get response headers in Restangular getList

my Rails API scrolls the pagination data in the response headers as follows:

X-Pagination {"total":332,"total_pages":12,"first_page":true,"last_page":false,"previous_page":null,"next_page":2,"out_of_bounds":false,"offset":0} 

I need to access this value while calling getList () to display paginator, etc.

I saw the headlines are blank.

How can I solve this problem?

Thanks Stefano

+7
pagination restangular
source share
3 answers

You can use the setFullResponse method, it will return all response data. I assume you need the complete answer as an exception, so just configure Restangular locally as follows:

 var rest = Restangular.withConfig(function(RestangularConfigurer) { RestangularConfigurer.setFullResponse(true); }); rest.getList().then(function(response) { console.log(response.headers('X-Pagination')); }); 
+9
source share

To add a 2j2e answer, I created a factory, and I use it as follows:

 angular.module('myApp') .factory('RestFullResponse', ['Restangular', function (Restangular) { return Restangular.withConfig(function (RestangularConfigurer) { RestangularConfigurer.setFullResponse(true); }); }]); 

And in the controller:

 angular.module('myApp') .controller('PikachuCtrl', function ($scope, Restangular, RestFullResponse) { var ideas = RestFullResponse.all('Pikachus'); ideas.getList().then(function(response){ $scope.Pikachus = response.data; var XPagination = response.headers('X-Pagination'); }; }) 
+4
source share

If you want to read only certain response headers, and you do not need complete answers with a heading in all relay requests, you can find the heading in the interceptor and add it to the response.

 app.config(function(RestangularProvider) { RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) { var pagination; if (pagination = response.headers('X-Pagination')) { data.paginationData= pagination; } return data; }); }); 
0
source share

All Articles