Superagent: Handle arrays as query parameter?

Created on 17 Apr 2015  路  7Comments  路  Source: visionmedia/superagent

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?

Most helpful comment

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.

All 7 comments

I'll make a PR for this.

could we also have an option for both forms:

// GET "/things?thing[]=a&thing[]=b&thing[]=c"
// GET "/things?thing=a&thing=b&thing=c"

I actually gotta :-1: this since I'm not a big fan of polluting the API. There's also a[0]=1&a[1]=2&a[2]=3 (this maintains the order of the array since key=value is by nature unordered. Some API takes in a=1,2,3 also. If you want to serialize an array, just serialize it and pass it in as the string. This works:

superagent()
  .get('/things')
  // String as query
  .query('a=1&a=2&a=3')
  // Object as query
  .query({ foo: bar })

It will concat things properly.
There's querystring libs like qs that handle a slew of diff serialization schemes.

@longlho the issue is that the node version of superagent and the client lib have different behaviors

node outputs a=1&a=2
client outputs a=1,2

yup client basically shims that. Again, you can pass in a serialized string to ensure proper serialization IMO.

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.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tbo picture tbo  路  4Comments

ariemeow picture ariemeow  路  8Comments

littlee picture littlee  路  5Comments

t3hmrman picture t3hmrman  路  9Comments

srohde picture srohde  路  8Comments