querystring.parse() has unexpected results with valueless keys #3058

Closed
laverdet opened this Issue Apr 4, 2012 · 5 comments

Comments

Projects
None yet
4 participants

laverdet commented Apr 4, 2012

5e3ca98 changed the behavior of querystring.parse() in the case where there's no value to a key in a querystring.

Before:

require('querystring').parse('foo&bar=1&etc')
{ foo: '', bar: '1', etc: '' }

After:

require('querystring').parse('foo&bar=1&etc')
{ '': [ 'foo', 'etc' ],
bar: '1' }

Since the message of the commit seems to claim the change was purely motivated by performance concerns I think this is a bug. Also the behavior is very odd indeed.

mscdex commented Apr 4, 2012

There is an easy fix, but after thinking about it, should we really be treating 'foo' and 'foo=' the same (as a key with no value)?

laverdet commented Apr 4, 2012

The way I do it internally is "?foo" is {foo: true} and "?foo=" is {foo:''}. You would need to update stringify to be consistent with parse though.

isaacs commented Apr 6, 2012

This is a bug. It should match v0.6.

isaacs closed this in a811a4a May 11, 2012

neverfox commented Dec 4, 2014

The way I do it internally is "?foo" is {foo: true} and "?foo=" is {foo:''}. You would need to update stringify to be consistent with parse though.

This doesn't appear to have been done. If I stringify { api: '', uid: '1234', pwd: 'abc123' }, I get api=&uid=1234&pwd=abc123 (though parse gives me the aforementioned object), which will get at 404 from the API I'm trying to write against (which I didn't design). So the two methods aren't symmetrically consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment