11cb0ef41Sopenharmony_ci# Query string
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci<!--introduced_in=v0.1.25-->
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci> Stability: 2 - Stable
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci<!--name=querystring-->
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci<!-- source_link=lib/querystring.js -->
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciThe `node:querystring` module provides utilities for parsing and formatting URL
121cb0ef41Sopenharmony_ciquery strings. It can be accessed using:
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci```js
151cb0ef41Sopenharmony_ciconst querystring = require('node:querystring');
161cb0ef41Sopenharmony_ci```
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci`querystring` is more performant than {URLSearchParams} but is not a
191cb0ef41Sopenharmony_cistandardized API. Use {URLSearchParams} when performance is not critical or
201cb0ef41Sopenharmony_ciwhen compatibility with browser code is desirable.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci## `querystring.decode()`
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci<!-- YAML
251cb0ef41Sopenharmony_ciadded: v0.1.99
261cb0ef41Sopenharmony_ci-->
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciThe `querystring.decode()` function is an alias for `querystring.parse()`.
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci## `querystring.encode()`
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci<!-- YAML
331cb0ef41Sopenharmony_ciadded: v0.1.99
341cb0ef41Sopenharmony_ci-->
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciThe `querystring.encode()` function is an alias for `querystring.stringify()`.
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci## `querystring.escape(str)`
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci<!-- YAML
411cb0ef41Sopenharmony_ciadded: v0.1.25
421cb0ef41Sopenharmony_ci-->
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci* `str` {string}
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ciThe `querystring.escape()` method performs URL percent-encoding on the given
471cb0ef41Sopenharmony_ci`str` in a manner that is optimized for the specific requirements of URL
481cb0ef41Sopenharmony_ciquery strings.
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ciThe `querystring.escape()` method is used by `querystring.stringify()` and is
511cb0ef41Sopenharmony_cigenerally not expected to be used directly. It is exported primarily to allow
521cb0ef41Sopenharmony_ciapplication code to provide a replacement percent-encoding implementation if
531cb0ef41Sopenharmony_cinecessary by assigning `querystring.escape` to an alternative function.
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci## `querystring.parse(str[, sep[, eq[, options]]])`
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci<!-- YAML
581cb0ef41Sopenharmony_ciadded: v0.1.25
591cb0ef41Sopenharmony_cichanges:
601cb0ef41Sopenharmony_ci  - version: v8.0.0
611cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/10967
621cb0ef41Sopenharmony_ci    description: Multiple empty entries are now parsed correctly (e.g. `&=&=`).
631cb0ef41Sopenharmony_ci  - version: v6.0.0
641cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/6055
651cb0ef41Sopenharmony_ci    description: The returned object no longer inherits from `Object.prototype`.
661cb0ef41Sopenharmony_ci  - version:
671cb0ef41Sopenharmony_ci    - v6.0.0
681cb0ef41Sopenharmony_ci    - v4.2.4
691cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/3807
701cb0ef41Sopenharmony_ci    description: The `eq` parameter may now have a length of more than `1`.
711cb0ef41Sopenharmony_ci-->
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci* `str` {string} The URL query string to parse
741cb0ef41Sopenharmony_ci* `sep` {string} The substring used to delimit key and value pairs in the
751cb0ef41Sopenharmony_ci  query string. **Default:** `'&'`.
761cb0ef41Sopenharmony_ci* `eq` {string}. The substring used to delimit keys and values in the
771cb0ef41Sopenharmony_ci  query string. **Default:** `'='`.
781cb0ef41Sopenharmony_ci* `options` {Object}
791cb0ef41Sopenharmony_ci  * `decodeURIComponent` {Function} The function to use when decoding
801cb0ef41Sopenharmony_ci    percent-encoded characters in the query string. **Default:**
811cb0ef41Sopenharmony_ci    `querystring.unescape()`.
821cb0ef41Sopenharmony_ci  * `maxKeys` {number} Specifies the maximum number of keys to parse.
831cb0ef41Sopenharmony_ci    Specify `0` to remove key counting limitations. **Default:** `1000`.
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ciThe `querystring.parse()` method parses a URL query string (`str`) into a
861cb0ef41Sopenharmony_cicollection of key and value pairs.
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ciFor example, the query string `'foo=bar&abc=xyz&abc=123'` is parsed into:
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci<!-- eslint-skip -->
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci```js
931cb0ef41Sopenharmony_ci{
941cb0ef41Sopenharmony_ci  foo: 'bar',
951cb0ef41Sopenharmony_ci  abc: ['xyz', '123']
961cb0ef41Sopenharmony_ci}
971cb0ef41Sopenharmony_ci```
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ciThe object returned by the `querystring.parse()` method _does not_
1001cb0ef41Sopenharmony_ciprototypically inherit from the JavaScript `Object`. This means that typical
1011cb0ef41Sopenharmony_ci`Object` methods such as `obj.toString()`, `obj.hasOwnProperty()`, and others
1021cb0ef41Sopenharmony_ciare not defined and _will not work_.
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ciBy default, percent-encoded characters within the query string will be assumed
1051cb0ef41Sopenharmony_cito use UTF-8 encoding. If an alternative character encoding is used, then an
1061cb0ef41Sopenharmony_cialternative `decodeURIComponent` option will need to be specified:
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci```js
1091cb0ef41Sopenharmony_ci// Assuming gbkDecodeURIComponent function already exists...
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ciquerystring.parse('w=%D6%D0%CE%C4&foo=bar', null, null,
1121cb0ef41Sopenharmony_ci                  { decodeURIComponent: gbkDecodeURIComponent });
1131cb0ef41Sopenharmony_ci```
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci## `querystring.stringify(obj[, sep[, eq[, options]]])`
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci<!-- YAML
1181cb0ef41Sopenharmony_ciadded: v0.1.25
1191cb0ef41Sopenharmony_ci-->
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci* `obj` {Object} The object to serialize into a URL query string
1221cb0ef41Sopenharmony_ci* `sep` {string} The substring used to delimit key and value pairs in the
1231cb0ef41Sopenharmony_ci  query string. **Default:** `'&'`.
1241cb0ef41Sopenharmony_ci* `eq` {string}. The substring used to delimit keys and values in the
1251cb0ef41Sopenharmony_ci  query string. **Default:** `'='`.
1261cb0ef41Sopenharmony_ci* `options`
1271cb0ef41Sopenharmony_ci  * `encodeURIComponent` {Function} The function to use when converting
1281cb0ef41Sopenharmony_ci    URL-unsafe characters to percent-encoding in the query string. **Default:**
1291cb0ef41Sopenharmony_ci    `querystring.escape()`.
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ciThe `querystring.stringify()` method produces a URL query string from a
1321cb0ef41Sopenharmony_cigiven `obj` by iterating through the object's "own properties".
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ciIt serializes the following types of values passed in `obj`:
1351cb0ef41Sopenharmony_ci{string|number|bigint|boolean|string\[]|number\[]|bigint\[]|boolean\[]}
1361cb0ef41Sopenharmony_ciThe numeric values must be finite. Any other input values will be coerced to
1371cb0ef41Sopenharmony_ciempty strings.
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci```js
1401cb0ef41Sopenharmony_ciquerystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' });
1411cb0ef41Sopenharmony_ci// Returns 'foo=bar&baz=qux&baz=quux&corge='
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ciquerystring.stringify({ foo: 'bar', baz: 'qux' }, ';', ':');
1441cb0ef41Sopenharmony_ci// Returns 'foo:bar;baz:qux'
1451cb0ef41Sopenharmony_ci```
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ciBy default, characters requiring percent-encoding within the query string will
1481cb0ef41Sopenharmony_cibe encoded as UTF-8. If an alternative encoding is required, then an alternative
1491cb0ef41Sopenharmony_ci`encodeURIComponent` option will need to be specified:
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci```js
1521cb0ef41Sopenharmony_ci// Assuming gbkEncodeURIComponent function already exists,
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ciquerystring.stringify({ w: '中文', foo: 'bar' }, null, null,
1551cb0ef41Sopenharmony_ci                      { encodeURIComponent: gbkEncodeURIComponent });
1561cb0ef41Sopenharmony_ci```
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci## `querystring.unescape(str)`
1591cb0ef41Sopenharmony_ci
1601cb0ef41Sopenharmony_ci<!-- YAML
1611cb0ef41Sopenharmony_ciadded: v0.1.25
1621cb0ef41Sopenharmony_ci-->
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci* `str` {string}
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_ciThe `querystring.unescape()` method performs decoding of URL percent-encoded
1671cb0ef41Sopenharmony_cicharacters on the given `str`.
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ciThe `querystring.unescape()` method is used by `querystring.parse()` and is
1701cb0ef41Sopenharmony_cigenerally not expected to be used directly. It is exported primarily to allow
1711cb0ef41Sopenharmony_ciapplication code to provide a replacement decoding implementation if
1721cb0ef41Sopenharmony_cinecessary by assigning `querystring.unescape` to an alternative function.
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ciBy default, the `querystring.unescape()` method will attempt to use the
1751cb0ef41Sopenharmony_ciJavaScript built-in `decodeURIComponent()` method to decode. If that fails,
1761cb0ef41Sopenharmony_cia safer equivalent that does not throw on malformed URLs will be used.
177