2012-02-27 20:09:34 +01:00
|
|
|
# Query String
|
|
|
|
|
2015-02-25 01:15:26 +01:00
|
|
|
Stability: 2 - Stable
|
2012-03-03 00:14:03 +01:00
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
<!--name=querystring-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
This module provides utilities for dealing with query strings.
|
|
|
|
It provides the following methods:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:26:30 +01:00
|
|
|
## querystring.escape
|
2016-05-05 07:50:03 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.25
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:26:30 +01:00
|
|
|
The escape function used by `querystring.stringify`,
|
|
|
|
provided so that it could be overridden if necessary.
|
2014-04-29 03:40:40 +02:00
|
|
|
|
2014-09-30 01:32:34 +02:00
|
|
|
## querystring.parse(str[, sep][, eq][, options])
|
2016-05-05 07:50:03 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.25
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
Deserialize a query string to an object.
|
2011-12-27 09:43:58 +01:00
|
|
|
Optionally override the default separator (`'&'`) and assignment (`'='`)
|
|
|
|
characters.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-01-15 20:45:31 +01:00
|
|
|
Options object may contain `maxKeys` property (equal to 1000 by default), it'll
|
|
|
|
be used to limit processed keys. Set it to 0 to remove key count limitation.
|
|
|
|
|
2015-02-13 04:59:11 +01:00
|
|
|
Options object may contain `decodeURIComponent` property (`querystring.unescape` by default),
|
2015-02-21 01:03:14 +01:00
|
|
|
it can be used to decode a `non-utf8` encoding string if necessary.
|
2014-04-29 03:40:40 +02:00
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
Example:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
querystring.parse('foo=bar&baz=qux&baz=quux&corge')
|
2016-03-14 22:48:26 +01:00
|
|
|
// returns { foo: 'bar', baz: ['qux', 'quux'], corge: '' }
|
2016-01-17 18:39:07 +01:00
|
|
|
|
|
|
|
// Suppose gbkDecodeURIComponent function already exists,
|
|
|
|
// it can decode `gbk` encoding string
|
|
|
|
querystring.parse('w=%D6%D0%CE%C4&foo=bar', null, null,
|
|
|
|
{ decodeURIComponent: gbkDecodeURIComponent })
|
2016-03-14 22:48:26 +01:00
|
|
|
// returns { w: '中文', foo: 'bar' }
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2014-04-29 03:40:40 +02:00
|
|
|
|
2015-11-04 18:26:30 +01:00
|
|
|
## querystring.stringify(obj[, sep][, eq][, options])
|
2016-05-05 07:50:03 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.25
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:26:30 +01:00
|
|
|
Serialize an object to a query string.
|
|
|
|
Optionally override the default separator (`'&'`) and assignment (`'='`)
|
|
|
|
characters.
|
|
|
|
|
|
|
|
Options object may contain `encodeURIComponent` property (`querystring.escape` by default),
|
|
|
|
it can be used to encode string with `non-utf8` encoding if necessary.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' })
|
2016-03-14 22:48:26 +01:00
|
|
|
// returns 'foo=bar&baz=qux&baz=quux&corge='
|
2016-01-17 18:39:07 +01:00
|
|
|
|
|
|
|
querystring.stringify({foo: 'bar', baz: 'qux'}, ';', ':')
|
2016-03-14 22:48:26 +01:00
|
|
|
// returns 'foo:bar;baz:qux'
|
2016-01-17 18:39:07 +01:00
|
|
|
|
|
|
|
// Suppose gbkEncodeURIComponent function already exists,
|
|
|
|
// it can encode string with `gbk` encoding
|
|
|
|
querystring.stringify({ w: '中文', foo: 'bar' }, null, null,
|
|
|
|
{ encodeURIComponent: gbkEncodeURIComponent })
|
2016-03-14 22:48:26 +01:00
|
|
|
// returns 'w=%D6%D0%CE%C4&foo=bar'
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
## querystring.unescape
|
2016-05-05 07:50:03 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.25
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
The unescape function used by `querystring.parse`,
|
|
|
|
provided so that it could be overridden if necessary.
|
2015-02-21 01:03:14 +01:00
|
|
|
|
|
|
|
It will try to use `decodeURIComponent` in the first place,
|
|
|
|
but if that fails it falls back to a safer equivalent that
|
|
|
|
doesn't throw on malformed URLs.
|