27

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 ?

2
  • Out of curiosity, where or how are you seeing what the query output is? Commented Apr 6, 2015 at 10:30
  • @Dan Jimenez Network tab in developer tools in any web browser :) That should do the trick. Commented Feb 4, 2016 at 16:51

4 Answers 4

19

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 ?

Sign up to request clarification or add additional context in comments.

Comments

11

Try this:

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

1 Comment

This answer is how it should work - using Restangular 1.4.0 and nested array parameters not being converted into a proper http query string. Have you been able to get the code above to work?
7

if you have very few and controlled parameters, you can use this way.

Assuming that you have few filters:

    var api = Restangular.all('yourEntityName');
    var params = {  commonWay          : 'value1',
                   'filter[property1]' : filterVariable1,
                   'filter[property2]' : filterVariable2
                 };

    api.getList(params).then(function (data) {
        alert(data);
    });

I hope this help you.

Comments

7

stringify the content using JSON

{
  "startkey": JSON.stringify(["Forum-03fa10f4-cefc-427a-9d57-f53bae4a0f7e"]),
  "endkey": JSON.stringify(["Forum-03fa10f4-cefc-427a-9d57-f53bae4a0f7e", {}]),
}

translates to

?endkey=%5B"Forum-03fa10f4-cefc-427a-9d57-f53bae4a0f7e",+%7B%7D%5D&startkey=%5B"Forum-03fa10f4-cefc-427a-9d57-f53bae4a0f7e"%5D

i.e.

?endkey=["Forum-03fa10f4-cefc-427a-9d57-f53bae4a0f7e",{}]&startkey=["Forum-03fa10f4-cefc-427a-9d57-f53bae4a0f7e"]

1 Comment

Thanks for posting, this is the most direct solution to this problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.