qs
qs copied to clipboard
Object with array containing object and scalars is not parsed correctly.
Client request from any browser $.get('/ajax', {arr: [{foo: 123, bar: 'foo'},2,3] },
...
On server (node .10, 4.x and 7.x) with latest qs
is parsed as
{ arr: { __0: [{ foo: 123, bar: 'foo', 2: true}, 3] } }
it should be
{ arr: [ {foo: 123, bar: 'foo'}, 2, 3 ] }
jQuery uses $.param
internally, which produces "arr%5B0%5D%5Bfoo%5D=123&arr%5B0%5D%5Bbar%5D=foo&arr%5B%5D=2&arr%5B%5D=3"
for your example.
What I get with the latest qs is q.parse('arr%5B0%5D%5Bfoo%5D=123&arr%5B0%5D%5Bbar%5D=foo&arr%5B%5D=2&arr%5B%5D=3')
producing { arr: [ { '2': true, foo: '123', bar: 'foo' }, '3' ] }
, which while different from what you expect, is also not the same as what you report.
Can you share the qs.parse
call code you're using, as well as the actual query string output that you're passing into it?
arr%5B__0%5D%5B0%5D%5Bfoo%5D=123&arr%5B__0%5D%5B0%5D%5Bbar%5D=foo&arr%5B__0%5D%5B%5D=2&arr%5B__0%5D%5B%5D=3
From a POST
qs.parse(req.body)
Sorry I had to transpose the parsed result from a WebStorm debugger But you see the problem.
As you can see, that string has __0
in it - I'm not sure where that comes from, but that could be messing things up.
_0 marks the array. That's coming from the browser URI encoding the object.
As you can see from my $.param
output above, that's not at all how the browser encodes it - __0
shouldn't ever appear unless you typed that out or a framework you use injected it - I'd love to get more information from you about the exact content of the Network tab in the browser (so you know with certainty what the browser is sending), to compare it to the query string you provided here.
Sample project that reproduces attached.
To run
extract $ npm install $ npm start
visit http://localhost:3000/
press do it
notice consle.
Code is at qsbug/routes/index.js // ##LOOK HERE##
Thanks, this will take me a bit to get to, but I appreciate the repro code :-)