Should URLSearchParams be used in node.js 8 instead of querystring?
See original GitHub issueNode >= 8.0 introduced an issue in querystring.parse (see here https://github.com/nodejs/node/issues/13773) that will affect all users of body-parser. It occurs when there is a trailing whitespace in a query string parameter:
Before require('querystring').parse('a=%20+&')
was { a: ' ' }
In node 8.x require('querystring').parse('a=%20+&')
is { a: '%20 ' }
This applies to any parameter value ending with a space.
Even though it is not body-parser’s fault, this issue can cause non trivial problems that are very hard to track down. By updating any app that uses body-parser on older node versions to node 8.x, any url-encoded form input with a trailing space will start to have the encoding of special characters broken. It took us a long time to discover the issue and to find out where the problem was. In the issue mentioned above, it is suggested that the new URLSearchParams class is a faster way of parsing query strings and more similar to native browser implementations. Does it make sense to consider it?
Issue Analytics
- State:
- Created 6 years ago
- Comments:23 (9 by maintainers)
Top GitHub Comments
blows the dust off
I was trying to match the behavior of
bodyParser.urlencoded
with node’surl.searchParams
and stumbled upon this issue…Is it time to revisit this discussion?
No,
URLSearchParams
has a different API. It has the exact same interface as theURLSearchParams
exposed in browsers.params[key] = value
isparams.set(key, value)
Array.isArray(params[key]) ? params[key].push(value) : params[key] = [params[key], value]
isparams.append(key, value)
delete params[key]
isparams.delete(key)
key in params
isparams.has(key)
params[key]
isparams.get(key)
orparams.getAll(key)
Object.keys(params)
isArray.from(params.keys())
(with the caveat that the latter would return duplicate keys)querystring.stringify(params)
isparams.toString()
It would not be possible for us to add any options not already in the spec to the
URLSearchParams
constructor. But we might make a custom API likeurl.parseSearchParams()
that takes options, much like the customurl.format()
we have forURL
. Even if we do add such an API, in general I would still encourage the use ofURLSearchParams
class if possible as it has a more defined API in the entire JS world.I assume you are talking about decoding/parsing with custom charsets here.
Currently, the Node.js core only has Latin-1/UTF-8/UTF-16 decoders built-in. Based on this and WHATWG’s stance
the
URLSearchParams
constructor will probably never support_charset_
. But when https://github.com/nodejs/node/pull/13644 lands, and if we do decide to add the aforementioned custom parser API, supporting_charset_
should be easy enough on our side to add an option for.