How to send an array of parameter through GET with Restangular

Issue

I have to send an array of filters through get parameters in an API like this :

/myList?filters[nickname]=test&filters[status]=foo

Now if I send an object directly like this :

Restangular.one('myList').get({filters: {
    nickname: 'test',
    status: 'foo'
}});

The query really sent is

?filters={"nickname":"test","status":"foo"}

How to send a real array ?
Should I thinks about an alternative ?

Solution

I found a way to do it, I have to iterate over the filter object to create a new object with the [] in the name :

var query = {};
for (var i in filters) {
    query['filters['+i+']'] = filters[i];
}

Restangular.one('myList').get(query);

Produce:

&filters%5Bnickname%5D=test

Someone have better solution ?

Answered By – Jihel

Answer Checked By – Willingham (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.