Express: misbehavior for query arrays

Created on 31 Jan 2017  路  1Comment  路  Source: expressjs/express

When i try run request like this:

site.com/?gf[1]=1,2,3&gf[22]=67,68

i get:

req.query.gf = {"1" : "1,2,3",  "22" : "67,68"}

I think,this is right.
But, if i run site/?gf[1]=1,2,3 (only one gf section), i get Array, not object:

req.query.gf = ["1,2,3"]

It may be something like {"1" : "1,2,3"} ?

4.x question

Most helpful comment

Hi @allgorod that is now the qs module (https://www.npmjs.com/package/qs) works out-of-the-box and it is what we use for the query parsing. The behavior you are asking for is also provided by the qs module, and you can set up your Express to use that different behavior as well:

// add before your first app.use
var qs = require('qs')
app.set('query parser', function (str) {
  // you can set whatever qs behavior you desire, or even use something other than qs
  return qs.parse(str, { arrayLimit: 0 })
})

You can find more information in our application settings docs as well: https://expressjs.com/en/4x/api.html#app.set

I hope this helps!

>All comments

Hi @allgorod that is now the qs module (https://www.npmjs.com/package/qs) works out-of-the-box and it is what we use for the query parsing. The behavior you are asking for is also provided by the qs module, and you can set up your Express to use that different behavior as well:

// add before your first app.use
var qs = require('qs')
app.set('query parser', function (str) {
  // you can set whatever qs behavior you desire, or even use something other than qs
  return qs.parse(str, { arrayLimit: 0 })
})

You can find more information in our application settings docs as well: https://expressjs.com/en/4x/api.html#app.set

I hope this helps!

Was this page helpful?
0 / 5 - 0 ratings