Skip to content

Send array of params on GET #700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tyler-boyd opened this issue May 7, 2014 · 7 comments
Closed

Send array of params on GET #700

tyler-boyd opened this issue May 7, 2014 · 7 comments
Labels

Comments

@tyler-boyd
Copy link

Hi, my issue is as follows:

I do a GET request where the parameters include arrays. Eg:

GET /products?query=mug&categories=drinkware&categories=coffee

The server sees:

PARAMS: 
{ 
"query" => "mug",
"categories" => ["drinkware", "coffee"]
}

How can I generate an address like that?

Currently I'm doing

searchProducts: (query, categories) ->
      params = { query: query }
      _.each categories, (cat, index) ->
        params['categories'] = cat.name
      Restangular.all('products').getList(params)```

But this only sends the last category as the parameter. I also tried:

```coffee
    searchProducts: (query, categories) ->
      params = { query: query }
      _.each categories, (cat, index) ->
        params['categories[' + index + ']'] = cat.name
      Restangular.all('products').getList(params)

But the server receives that as an array of hashes

{
"query" => "mug",
"categories" => { "0" => "drinkware", "1" => "coffee" }
}

Any advice would be greatly appreciated!

I know that I could solve the issue by changing how I process the params (I could do a map down into an array, I could just do categories=JSON.stringify(category_names) in the client, but I'll bet there's a pretty solution I'm missing.

Thank you.

@mgonto
Copy link
Owner

mgonto commented May 30, 2014

Hey,

I send those Request Parameters actually to AngularJS.

They've just fixed that problem and you can do what you're asking the following way now: angular/angular.js#1364

Hope it works out :). Let me know if it does!

Cheers

@CLOUGH
Copy link

CLOUGH commented Oct 7, 2015

I recently ran into this issue and i had to do a bit of research to resolve the problem. The issue was closed but for anyone that might stumble on this problem here is a solution.

If you want to pass an array of items within angular all you need to do is store the property name with a open and close square brace:

$scope.filters = {
   "title" : 'some value',
   "type[]": ['Full Time','Part Time']
};

Restangular.all('jobs').getList($scope.filters);

I hope this help.

@lognwching
Copy link

@CLOUGH thanks a lot man! Your comment just saved me! 🙌 🚀

@gbittmann
Copy link

Using the method described by @CLOUGH the braces are converted to %5B%5D, which does not work.

$scope.filter = { 'tags[]': ['live','design'] }; Restangular.all('search').getList($scope.filter);

@daviesgeek
Copy link
Collaborator

@gbittmann Can you please provide a live example on Plunker, JSFiddle or the like?

@KKrisu
Copy link

KKrisu commented May 15, 2017

I confirm the issue that @gbittmann has.
In my case, I've just passed simple key: array format:

const params = {
    country: ['fr', 'pl'],
};

Restangular.all('jobs').getList(params);

This generates url with &country=fr&country=pl

@gbittmann
Copy link

Sorry. I lost track of this somehow.

https://plnkr.co/edit/Ig2qWW4FfL2r1nkIpqtx?p=preview

You can open up the console and see that it is posting the encoded version of the brackets. Which is probably what it should be doing according to the http spec, but causes issues when you are posting to a php server and getting to accept that as an array. An option to force the encoding to not happen would probably be the only thing that could be done. The current behavior seems to be correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants