Is it possible to do a Restangular GET with multiple query parameters?

I'm trying to do a Restangular GET with multiple query parameters (for simple pagination) and it doesn't seem possible - at least not with what I'm trying to do. Here's what I'm trying to do:

Restangular.all('elevation').get({ pageParam: page, pageSizeParam: pageSize }).then(function(data) {
    console.log(data);
});

      

With an expected response that looks something like this:

{ totalRecords: 233, elevations: {...} }

      

It doesn't work and results in the following:

GET http: // localhost: 24287 / elevation / [object% 20Object] 404 (not found)

I am also trying to use customGET

which leads to the same problem as above.

The only way I can pass multiple query parameters is by using getList

. Unfortunately, when used getList

unsurprisingly, the following error occurs:

Error: The response for getList SHOULD be an array, not an object or something

To fix this issue, the documentation for the Standard Documentation procedure states "My Answer" is actually wrapped in some metadata. How can I get the data in this case? which I need to use addResponseInterceptor

, which I did like this:

 RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
  var newResponse = [];
  if(response.data.totalRecords !== undefined) {
    newResponse.elevationData= {};
    newResponse.elevationData.totalRecords = response.data.totalRecords;
    newResponse.elevationData.elevations = response.data.elevations;
  }
  return newResponse;
});

      

After jumping through the hoops, this convoluted solution actually works. Isn't it easier to just call Restangular get

with multiple query parameters and return an object?

At this point, it would be easier to take Restangular from the question and just use the $ http service like so:

$http.get('http://localhost:24287/elevation?page=' + page + '&pageSize=' + pageSize).then(function(result) {
    console.log(result.data.totalRecords);
    console.log(result.data.elevations);
});

      

Though I really want to find a better way to do this with Restangular.

Any ideas would be greatly appreciated! Thank!

+3


source to share


1 answer


Which is deadly simple, requesting a ressource url with no id, even with specific query parameters and returning a single object, is just not RESTful.

Error: The response for getList SHOULD be an array, not an object or something

This tells you the truth for sure: your query might return an array because you didn't provide a unique ID (everything else is not unique).



At this point it would be easier to take Restangular from the question and just use the $ http service

Definitely since Restangular focuses on the RESTful proxy resource, or you will need to configure it (interceptors, customGET, ...)

+1


source







All Articles