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.

Handle arrays as query parameter?

See original GitHub issue

Say I need a function that works like this:

getThings(['a', 'b', 'c'], cb)  // GET "/things?thing[]=a&thing[]=b&thing[]=c"

Currently, I have to do this:

function getThings(things, cb) {
    var req = superagent.get('/things')
    for (var i = 0; i < things.length; i++) {
        req.query({thing: things[i]});
    }
    req.end(cb);
}

I would like to be able to do something like this instead:

function getThings(things, cb) {
    superagent.get('/things')
       .query({thing: things})
       .end(cb);
}

Alternatively, use a different name for the array-enabled query method:

function getThings(things, cb) {
    superagent.get('/things')
       .queryArray('thing', things)
       .end(cb);
}

Or is there a way to do this already, that I haven’t found?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
ropezcommented, Jun 5, 2015

Well, qs can format query strings from arrays with no sweat.

> var qs = require('qs')
> qs.stringify({foo: 10, things: [1,2,3]})
'foo=10&things%5B0%5D=1&things%5B1%5D=2&things%5B2%5D=3'
> qs.stringify({foo: 10, things: [1,2,3]}, {arrayFormat: 'repeat'})
'foo=10&things=1&things=2&things=3'
> qs.stringify({foo: 10, things: [1,2,3]}, {arrayFormat: 'brackets'})
'foo=10&things%5B%5D=1&things%5B%5D=2&things%5B%5D=3'

Can be used with superagent easily:

superagent.get('/things').query(qs.stringify({thing: things}))

However, I decided to use it with plain XMLHttpRequest in our project.

1reaction
daviscommented, Jun 9, 2015

my issue originally came up because i wrote a library that is supposed to work on both node and client and has superagent as a dependency. a user reported an issue when using the client lib which i could not reproduce with my mocha tests (node). that’s why i reported it.

client and node libs have different behaviors – i don’t think that should be the case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass an array within a query string? - Stack Overflow
I want that query string value to be treated as an array- I don't want the array to be exploded so that it...
Read more >
Arrays in query params - Medium
Arrays in query parameters are sometimes represented by repeating a ... each use-case, we'll see how to model that query parameter in RAML....
Read more >
Fun stuff: representing arrays and objects in query strings
Decoding a query string into an array is done with parse_str. ... Also: it looks like it handled the array parameter, but it...
Read more >
Use the Query String to pass an array of selected values ...
You can pass data, including arrays via the Query String when using NavigationManager to navigate to a different page in your Blazor app....
Read more >
Angular - How to pass arrays via Query Parameters
This is a quick guide on how to pass an array of values via query string in Angular. This is working in Angular...
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