question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Send array of params on GET

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

17reactions
CLOUGHcommented, 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.

1reaction
KKrisucommented, 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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass an array within a query string? - Stack Overflow
Here's what I figured out: Submitting multi-value form fields, i.e. submitting arrays through GET/POST vars, can be done several different ...
Read more >
Arrays in query params - Medium
Here is an example that leverages RAML Traits and Trait Parameters to define the fieldset that can be passed to a “fields” query...
Read more >
Fun stuff: representing arrays and objects in query strings
getAll('user_ids') , you'll get an array containing the string "1,3" as a single item, instead of an array with two separate items.
Read more >
Use the Query String to pass an array of selected values ...
Summary. You can pass data, including arrays via the Query String when using NavigationManager to navigate to a different page in your Blazor ......
Read more >
How to pass an array as a URL parameter | Edureka Community
I would like to pass an array and added to a link on my page as a URL parameter, because later on the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found