Query string#
Source Code:lib/querystring.js
Thenode:querystring module provides utilities for parsing and formatting URLquery strings. It can be accessed using:
const querystring =require('node:querystring');querystring is more performant than<URLSearchParams> but is not astandardized API. Use<URLSearchParams> when performance is not critical orwhen compatibility with browser code is desirable.
querystring.decode()#
Thequerystring.decode() function is an alias forquerystring.parse().
querystring.encode()#
Thequerystring.encode() function is an alias forquerystring.stringify().
querystring.escape(str)#
str<string>
Thequerystring.escape() method performs URL percent-encoding on the givenstr in a manner that is optimized for the specific requirements of URLquery strings.
Thequerystring.escape() method is used byquerystring.stringify() and isgenerally not expected to be used directly. It is exported primarily to allowapplication code to provide a replacement percent-encoding implementation ifnecessary by assigningquerystring.escape to an alternative function.
querystring.parse(str[, sep[, eq[, options]]])#
History
| Version | Changes |
|---|---|
| v8.0.0 | Multiple empty entries are now parsed correctly (e.g. |
| v6.0.0 | The returned object no longer inherits from |
| v6.0.0, v4.2.4 | The |
| v0.1.25 | Added in: v0.1.25 |
str<string> The URL query string to parsesep<string> The substring used to delimit key and value pairs in thequery string.Default:'&'.eq<string>. The substring used to delimit keys and values in thequery string.Default:'='.options<Object>decodeURIComponent<Function> The function to use when decodingpercent-encoded characters in the query string.Default:querystring.unescape().maxKeys<number> Specifies the maximum number of keys to parse.Specify0to remove key counting limitations.Default:1000.
Thequerystring.parse() method parses a URL query string (str) into acollection of key and value pairs.
For example, the query string'foo=bar&abc=xyz&abc=123' is parsed into:
{"foo":"bar","abc":["xyz","123"]}The object returned by thequerystring.parse() methoddoes notprototypically inherit from the JavaScriptObject. This means that typicalObject methods such asobj.toString(),obj.hasOwnProperty(), and othersare not defined andwill not work.
By default, percent-encoded characters within the query string will be assumedto use UTF-8 encoding. If an alternative character encoding is used, then analternativedecodeURIComponent option will need to be specified:
// Assuming gbkDecodeURIComponent function already exists...querystring.parse('w=%D6%D0%CE%C4&foo=bar',null,null, {decodeURIComponent: gbkDecodeURIComponent });querystring.stringify(obj[, sep[, eq[, options]]])#
obj<Object> The object to serialize into a URL query stringsep<string> The substring used to delimit key and value pairs in thequery string.Default:'&'.eq<string>. The substring used to delimit keys and values in thequery string.Default:'='.optionsencodeURIComponent<Function> The function to use when convertingURL-unsafe characters to percent-encoding in the query string.Default:querystring.escape().
Thequerystring.stringify() method produces a URL query string from agivenobj by iterating through the object's "own properties".
It serializes the following types of values passed inobj:<string> |<number> |<bigint> |<boolean> |<string[]> |<number[]> |<bigint[]> |<boolean[]>The numeric values must be finite. Any other input values will be coerced toempty strings.
querystring.stringify({foo:'bar',baz: ['qux','quux'],corge:'' });// Returns 'foo=bar&baz=qux&baz=quux&corge='querystring.stringify({foo:'bar',baz:'qux' },';',':');// Returns 'foo:bar;baz:qux'By default, characters requiring percent-encoding within the query string willbe encoded as UTF-8. If an alternative encoding is required, then an alternativeencodeURIComponent option will need to be specified:
// Assuming gbkEncodeURIComponent function already exists,querystring.stringify({w:'中文',foo:'bar' },null,null, {encodeURIComponent: gbkEncodeURIComponent });querystring.unescape(str)#
str<string>
Thequerystring.unescape() method performs decoding of URL percent-encodedcharacters on the givenstr.
Thequerystring.unescape() method is used byquerystring.parse() and isgenerally not expected to be used directly. It is exported primarily to allowapplication code to provide a replacement decoding implementation ifnecessary by assigningquerystring.unescape to an alternative function.
By default, thequerystring.unescape() method will attempt to use theJavaScript built-indecodeURIComponent() method to decode. If that fails,a safer equivalent that does not throw on malformed URLs will be used.