WelcomeTo
WelcomeTo

Reputation: 20591

Sending GET parameters in Restangular

I'm using Restangular in my project and earlier this code worked well for retrieving array of objects:

var params = {name: "Stack", surname: "Overflow"}
var service = Restangular.all('users')
service.getList(params)

Response from server was just an array of objects:

[
 {...},
 {...}
]

But now I added pagination, and my response now contains not an array, but an object which includes array:

{
   totalCount: 500,
   data: [
     {...},
     {...}
   ]
}

Also I changed service.getList(params) to service.get(params) (because getList expects only arrays).

And after this changes my GET parameters are not stringified, i.e. I see in debugger request like this:

users/[object%20Object]

but earlier (when using getList method) it worked as I expected:

users?name=Stack&surname=Overflow

What is the problem here?

Upvotes: 10

Views: 18448

Answers (3)

perseus
perseus

Reputation: 1391

Just use one() and get(params):

var params = {name: "Stack", surname: "Overflow"};
var service = Restangular.one('users');
service.get(params).then(function(response) {
   console.log(response);
})

Upvotes: 0

Sebastián Lara
Sebastián Lara

Reputation: 5551

You can use customGETLIST method, look this:

// https://myapi.com/users?name=Stack&surname=Overflow

Restangular.all('users').customGETLIST('', {
    name: "Stack", 
    surname: "Overflow"
}).then(function (response) {
    console.log(response);
});

customGETLIST(path, [params, headers]): Does a GET to the specific path. In this case, you expect to get an array, not a single element. Optionally you can set params and headers.

Documentation: HERE

Good luck!!

Upvotes: 1

WelcomeTo
WelcomeTo

Reputation: 20591

I was able to solve it using this:

var params = {name: "Stack", surname: "Overflow"}
var service = Restangular.all('users')
service.customGET("", params) // first parameter is required, so just provide empty string

Upvotes: 14

Related Questions