qs icon indicating copy to clipboard operation
qs copied to clipboard

Object with array containing object and scalars is not parsed correctly.

Open edgework opened this issue 8 years ago • 7 comments

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 ] }

edgework avatar Feb 09 '17 00:02 edgework

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?

ljharb avatar Feb 09 '17 01:02 ljharb

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.

edgework avatar Feb 09 '17 01:02 edgework

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.

ljharb avatar Feb 09 '17 07:02 ljharb

_0 marks the array. That's coming from the browser URI encoding the object.

edgework avatar Feb 09 '17 17:02 edgework

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.

ljharb avatar Feb 09 '17 18:02 ljharb

qsbug.zip

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##

edgework avatar Feb 09 '17 18:02 edgework

Thanks, this will take me a bit to get to, but I appreciate the repro code :-)

ljharb avatar Feb 09 '17 18:02 ljharb