1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width"> 6 <meta name="nodejs.org:node-version" content="v18.20.1"> 7 <title>Query string | Node.js v18.20.1 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/querystring.html"> 12 <script async defer src="assets/api.js" type="text/javascript"></script> 13 14</head> 15<body class="alt apidoc" id="api-section-querystring"> 16 <div id="content" class="clearfix"> 17 <div id="column2" class="interior"> 18 <div id="intro" class="interior"> 19 <a href="/" title="Go back to the home page"> 20 Node.js 21 </a> 22 </div> 23 <ul> 24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 26</ul> 27<hr class="line"> 28<ul> 29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 33<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 38<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 39<li><a href="console.html" class="nav-console">Console</a></li> 40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 45<li><a href="dns.html" class="nav-dns">DNS</a></li> 46<li><a href="domain.html" class="nav-domain">Domain</a></li> 47<li><a href="errors.html" class="nav-errors">Errors</a></li> 48<li><a href="events.html" class="nav-events">Events</a></li> 49<li><a href="fs.html" class="nav-fs">File system</a></li> 50<li><a href="globals.html" class="nav-globals">Globals</a></li> 51<li><a href="http.html" class="nav-http">HTTP</a></li> 52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 53<li><a href="https.html" class="nav-https">HTTPS</a></li> 54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 55<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 60<li><a href="net.html" class="nav-net">Net</a></li> 61<li><a href="os.html" class="nav-os">OS</a></li> 62<li><a href="path.html" class="nav-path">Path</a></li> 63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 65<li><a href="process.html" class="nav-process">Process</a></li> 66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 67<li><a href="querystring.html" class="nav-querystring active">Query strings</a></li> 68<li><a href="readline.html" class="nav-readline">Readline</a></li> 69<li><a href="repl.html" class="nav-repl">REPL</a></li> 70<li><a href="report.html" class="nav-report">Report</a></li> 71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 72<li><a href="stream.html" class="nav-stream">Stream</a></li> 73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 74<li><a href="test.html" class="nav-test">Test runner</a></li> 75<li><a href="timers.html" class="nav-timers">Timers</a></li> 76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 78<li><a href="tty.html" class="nav-tty">TTY</a></li> 79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 80<li><a href="url.html" class="nav-url">URL</a></li> 81<li><a href="util.html" class="nav-util">Utilities</a></li> 82<li><a href="v8.html" class="nav-v8">V8</a></li> 83<li><a href="vm.html" class="nav-vm">VM</a></li> 84<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 89</ul> 90<hr class="line"> 91<ul> 92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 93</ul> 94 </div> 95 96 <div id="column1" data-id="querystring" class="interior"> 97 <header class="header"> 98 <div class="header-container"> 99 <h1>Node.js v18.20.1 documentation</h1> 100 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 101 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 102 <path fill="none" d="M0 0h24v24H0z" /> 103 <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> 104 <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> 105 </svg> 106 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 107 <path d="M0 0h24v24H0z" fill="none" /> 108 <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> 109 </svg> 110 </button> 111 </div> 112 <div id="gtoc"> 113 <ul> 114 <li class="pinned-header">Node.js v18.20.1</li> 115 116 <li class="picker-header"> 117 <a href="#"> 118 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 119 Table of contents 120 </a> 121 122 <div class="picker"><div class="toc"><ul> 123<li><span class="stability_2"><a href="#query-string">Query string</a></span> 124<ul> 125<li><a href="#querystringdecode"><code>querystring.decode()</code></a></li> 126<li><a href="#querystringencode"><code>querystring.encode()</code></a></li> 127<li><a href="#querystringescapestr"><code>querystring.escape(str)</code></a></li> 128<li><a href="#querystringparsestr-sep-eq-options"><code>querystring.parse(str[, sep[, eq[, options]]])</code></a></li> 129<li><a href="#querystringstringifyobj-sep-eq-options"><code>querystring.stringify(obj[, sep[, eq[, options]]])</code></a></li> 130<li><a href="#querystringunescapestr"><code>querystring.unescape(str)</code></a></li> 131</ul> 132</li> 133</ul></div></div> 134 </li> 135 136 137 <li class="picker-header"> 138 <a href="#"> 139 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 140 Index 141 </a> 142 143 <div class="picker"><ul> 144<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 145<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 146 147 <li> 148 <a href="index.html">Index</a> 149 </li> 150 </ul> 151 152<hr class="line"> 153<ul> 154<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 155<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 156<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 157<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 158<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 159<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 160<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 161<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 162<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 163<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 164<li><a href="console.html" class="nav-console">Console</a></li> 165<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 166<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 167<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 168<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 169<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 170<li><a href="dns.html" class="nav-dns">DNS</a></li> 171<li><a href="domain.html" class="nav-domain">Domain</a></li> 172<li><a href="errors.html" class="nav-errors">Errors</a></li> 173<li><a href="events.html" class="nav-events">Events</a></li> 174<li><a href="fs.html" class="nav-fs">File system</a></li> 175<li><a href="globals.html" class="nav-globals">Globals</a></li> 176<li><a href="http.html" class="nav-http">HTTP</a></li> 177<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 178<li><a href="https.html" class="nav-https">HTTPS</a></li> 179<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 180<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 181<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 182<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 183<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 184<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 185<li><a href="net.html" class="nav-net">Net</a></li> 186<li><a href="os.html" class="nav-os">OS</a></li> 187<li><a href="path.html" class="nav-path">Path</a></li> 188<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 189<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 190<li><a href="process.html" class="nav-process">Process</a></li> 191<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 192<li><a href="querystring.html" class="nav-querystring active">Query strings</a></li> 193<li><a href="readline.html" class="nav-readline">Readline</a></li> 194<li><a href="repl.html" class="nav-repl">REPL</a></li> 195<li><a href="report.html" class="nav-report">Report</a></li> 196<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 197<li><a href="stream.html" class="nav-stream">Stream</a></li> 198<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 199<li><a href="test.html" class="nav-test">Test runner</a></li> 200<li><a href="timers.html" class="nav-timers">Timers</a></li> 201<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 202<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 203<li><a href="tty.html" class="nav-tty">TTY</a></li> 204<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 205<li><a href="url.html" class="nav-url">URL</a></li> 206<li><a href="util.html" class="nav-util">Utilities</a></li> 207<li><a href="v8.html" class="nav-v8">V8</a></li> 208<li><a href="vm.html" class="nav-vm">VM</a></li> 209<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 210<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 211<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 212<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 213<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 214</ul> 215<hr class="line"> 216<ul> 217<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 218</ul></div> 219 </li> 220 221 222 <li class="picker-header"> 223 <a href="#"> 224 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 225 Other versions 226 </a> 227 <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/querystring.html">21.x</a></li> 228<li><a href="https://nodejs.org/docs/latest-v20.x/api/querystring.html">20.x <b>LTS</b></a></li> 229<li><a href="https://nodejs.org/docs/latest-v19.x/api/querystring.html">19.x</a></li> 230<li><a href="https://nodejs.org/docs/latest-v18.x/api/querystring.html">18.x <b>LTS</b></a></li> 231<li><a href="https://nodejs.org/docs/latest-v17.x/api/querystring.html">17.x</a></li> 232<li><a href="https://nodejs.org/docs/latest-v16.x/api/querystring.html">16.x</a></li> 233<li><a href="https://nodejs.org/docs/latest-v15.x/api/querystring.html">15.x</a></li> 234<li><a href="https://nodejs.org/docs/latest-v14.x/api/querystring.html">14.x</a></li> 235<li><a href="https://nodejs.org/docs/latest-v13.x/api/querystring.html">13.x</a></li> 236<li><a href="https://nodejs.org/docs/latest-v12.x/api/querystring.html">12.x</a></li> 237<li><a href="https://nodejs.org/docs/latest-v11.x/api/querystring.html">11.x</a></li> 238<li><a href="https://nodejs.org/docs/latest-v10.x/api/querystring.html">10.x</a></li> 239<li><a href="https://nodejs.org/docs/latest-v9.x/api/querystring.html">9.x</a></li> 240<li><a href="https://nodejs.org/docs/latest-v8.x/api/querystring.html">8.x</a></li> 241<li><a href="https://nodejs.org/docs/latest-v7.x/api/querystring.html">7.x</a></li> 242<li><a href="https://nodejs.org/docs/latest-v6.x/api/querystring.html">6.x</a></li> 243<li><a href="https://nodejs.org/docs/latest-v5.x/api/querystring.html">5.x</a></li> 244<li><a href="https://nodejs.org/docs/latest-v4.x/api/querystring.html">4.x</a></li> 245<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/querystring.html">0.12.x</a></li> 246<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/querystring.html">0.10.x</a></li></ol></div> 247 </li> 248 249 <li class="picker-header"> 250 <a href="#"> 251 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 252 Options 253 </a> 254 255 <div class="picker"> 256 <ul> 257 <li> 258 <a href="all.html">View on single page</a> 259 </li> 260 <li> 261 <a href="querystring.json">View as JSON</a> 262 </li> 263 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/querystring.md">Edit on GitHub</a></li> 264 </ul> 265 </div> 266 </li> 267 </ul> 268 </div> 269 <hr> 270 </header> 271 272 <details id="toc" open><summary>Table of contents</summary><ul> 273<li><span class="stability_2"><a href="#query-string">Query string</a></span> 274<ul> 275<li><a href="#querystringdecode"><code>querystring.decode()</code></a></li> 276<li><a href="#querystringencode"><code>querystring.encode()</code></a></li> 277<li><a href="#querystringescapestr"><code>querystring.escape(str)</code></a></li> 278<li><a href="#querystringparsestr-sep-eq-options"><code>querystring.parse(str[, sep[, eq[, options]]])</code></a></li> 279<li><a href="#querystringstringifyobj-sep-eq-options"><code>querystring.stringify(obj[, sep[, eq[, options]]])</code></a></li> 280<li><a href="#querystringunescapestr"><code>querystring.unescape(str)</code></a></li> 281</ul> 282</li> 283</ul></details> 284 285 <div id="apicontent"> 286 <h2>Query string<span><a class="mark" href="#query-string" id="query-string">#</a></span><a aria-hidden="true" class="legacy" id="querystring_query_string"></a></h2> 287 288<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 289 290<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/querystring.js">lib/querystring.js</a></p> 291<p>The <code>node:querystring</code> module provides utilities for parsing and formatting URL 292query strings. It can be accessed using:</p> 293<pre><code class="language-js"><span class="hljs-keyword">const</span> querystring = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:querystring'</span>);</code> <button class="copy-button">copy</button></pre> 294<p><code>querystring</code> is more performant than <a href="url.html#class-urlsearchparams" class="type"><URLSearchParams></a> but is not a 295standardized API. Use <a href="url.html#class-urlsearchparams" class="type"><URLSearchParams></a> when performance is not critical or 296when compatibility with browser code is desirable.</p> 297<section><h3><code>querystring.decode()</code><span><a class="mark" href="#querystringdecode" id="querystringdecode">#</a></span><a aria-hidden="true" class="legacy" id="querystring_querystring_decode"></a></h3> 298<div class="api_metadata"> 299<span>Added in: v0.1.99</span> 300</div> 301<p>The <code>querystring.decode()</code> function is an alias for <code>querystring.parse()</code>.</p> 302</section><section><h3><code>querystring.encode()</code><span><a class="mark" href="#querystringencode" id="querystringencode">#</a></span><a aria-hidden="true" class="legacy" id="querystring_querystring_encode"></a></h3> 303<div class="api_metadata"> 304<span>Added in: v0.1.99</span> 305</div> 306<p>The <code>querystring.encode()</code> function is an alias for <code>querystring.stringify()</code>.</p> 307</section><section><h3><code>querystring.escape(str)</code><span><a class="mark" href="#querystringescapestr" id="querystringescapestr">#</a></span><a aria-hidden="true" class="legacy" id="querystring_querystring_escape_str"></a></h3> 308<div class="api_metadata"> 309<span>Added in: v0.1.25</span> 310</div> 311<ul> 312<li><code>str</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 313</ul> 314<p>The <code>querystring.escape()</code> method performs URL percent-encoding on the given 315<code>str</code> in a manner that is optimized for the specific requirements of URL 316query strings.</p> 317<p>The <code>querystring.escape()</code> method is used by <code>querystring.stringify()</code> and is 318generally not expected to be used directly. It is exported primarily to allow 319application code to provide a replacement percent-encoding implementation if 320necessary by assigning <code>querystring.escape</code> to an alternative function.</p> 321</section><section><h3><code>querystring.parse(str[, sep[, eq[, options]]])</code><span><a class="mark" href="#querystringparsestr-sep-eq-options" id="querystringparsestr-sep-eq-options">#</a></span><a aria-hidden="true" class="legacy" id="querystring_querystring_parse_str_sep_eq_options"></a></h3> 322<div class="api_metadata"> 323<details class="changelog"><summary>History</summary> 324<table> 325<tbody><tr><th>Version</th><th>Changes</th></tr> 326<tr><td>v8.0.0</td> 327<td><p>Multiple empty entries are now parsed correctly (e.g. <code>&=&=</code>).</p></td></tr> 328<tr><td>v6.0.0</td> 329<td><p>The returned object no longer inherits from <code>Object.prototype</code>.</p></td></tr> 330<tr><td>v6.0.0, v4.2.4</td> 331<td><p>The <code>eq</code> parameter may now have a length of more than <code>1</code>.</p></td></tr> 332<tr><td>v0.1.25</td> 333<td><p><span>Added in: v0.1.25</span></p></td></tr> 334</tbody></table> 335</details> 336</div> 337<ul> 338<li><code>str</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The URL query string to parse</li> 339<li><code>sep</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The substring used to delimit key and value pairs in the 340query string. <strong>Default:</strong> <code>'&'</code>.</li> 341<li><code>eq</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a>. The substring used to delimit keys and values in the 342query string. <strong>Default:</strong> <code>'='</code>.</li> 343<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 344<ul> 345<li><code>decodeURIComponent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The function to use when decoding 346percent-encoded characters in the query string. <strong>Default:</strong> 347<code>querystring.unescape()</code>.</li> 348<li><code>maxKeys</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Specifies the maximum number of keys to parse. 349Specify <code>0</code> to remove key counting limitations. <strong>Default:</strong> <code>1000</code>.</li> 350</ul> 351</li> 352</ul> 353<p>The <code>querystring.parse()</code> method parses a URL query string (<code>str</code>) into a 354collection of key and value pairs.</p> 355<p>For example, the query string <code>'foo=bar&abc=xyz&abc=123'</code> is parsed into:</p> 356<!-- eslint-skip --> 357<pre><code class="language-js">{ 358 <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar'</span>, 359 <span class="hljs-attr">abc</span>: [<span class="hljs-string">'xyz'</span>, <span class="hljs-string">'123'</span>] 360}</code> <button class="copy-button">copy</button></pre> 361<p>The object returned by the <code>querystring.parse()</code> method <em>does not</em> 362prototypically inherit from the JavaScript <code>Object</code>. This means that typical 363<code>Object</code> methods such as <code>obj.toString()</code>, <code>obj.hasOwnProperty()</code>, and others 364are not defined and <em>will not work</em>.</p> 365<p>By default, percent-encoded characters within the query string will be assumed 366to use UTF-8 encoding. If an alternative character encoding is used, then an 367alternative <code>decodeURIComponent</code> option will need to be specified:</p> 368<pre><code class="language-js"><span class="hljs-comment">// Assuming gbkDecodeURIComponent function already exists...</span> 369 370querystring.<span class="hljs-title function_">parse</span>(<span class="hljs-string">'w=%D6%D0%CE%C4&foo=bar'</span>, <span class="hljs-literal">null</span>, <span class="hljs-literal">null</span>, 371 { <span class="hljs-attr">decodeURIComponent</span>: gbkDecodeURIComponent });</code> <button class="copy-button">copy</button></pre> 372</section><section><h3><code>querystring.stringify(obj[, sep[, eq[, options]]])</code><span><a class="mark" href="#querystringstringifyobj-sep-eq-options" id="querystringstringifyobj-sep-eq-options">#</a></span><a aria-hidden="true" class="legacy" id="querystring_querystring_stringify_obj_sep_eq_options"></a></h3> 373<div class="api_metadata"> 374<span>Added in: v0.1.25</span> 375</div> 376<ul> 377<li><code>obj</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> The object to serialize into a URL query string</li> 378<li><code>sep</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The substring used to delimit key and value pairs in the 379query string. <strong>Default:</strong> <code>'&'</code>.</li> 380<li><code>eq</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a>. The substring used to delimit keys and values in the 381query string. <strong>Default:</strong> <code>'='</code>.</li> 382<li><code>options</code> 383<ul> 384<li><code>encodeURIComponent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The function to use when converting 385URL-unsafe characters to percent-encoding in the query string. <strong>Default:</strong> 386<code>querystring.escape()</code>.</li> 387</ul> 388</li> 389</ul> 390<p>The <code>querystring.stringify()</code> method produces a URL query string from a 391given <code>obj</code> by iterating through the object's "own properties".</p> 392<p>It serializes the following types of values passed in <code>obj</code>: 393<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean[]></a> 394The numeric values must be finite. Any other input values will be coerced to 395empty strings.</p> 396<pre><code class="language-js">querystring.<span class="hljs-title function_">stringify</span>({ <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar'</span>, <span class="hljs-attr">baz</span>: [<span class="hljs-string">'qux'</span>, <span class="hljs-string">'quux'</span>], <span class="hljs-attr">corge</span>: <span class="hljs-string">''</span> }); 397<span class="hljs-comment">// Returns 'foo=bar&baz=qux&baz=quux&corge='</span> 398 399querystring.<span class="hljs-title function_">stringify</span>({ <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar'</span>, <span class="hljs-attr">baz</span>: <span class="hljs-string">'qux'</span> }, <span class="hljs-string">';'</span>, <span class="hljs-string">':'</span>); 400<span class="hljs-comment">// Returns 'foo:bar;baz:qux'</span></code> <button class="copy-button">copy</button></pre> 401<p>By default, characters requiring percent-encoding within the query string will 402be encoded as UTF-8. If an alternative encoding is required, then an alternative 403<code>encodeURIComponent</code> option will need to be specified:</p> 404<pre><code class="language-js"><span class="hljs-comment">// Assuming gbkEncodeURIComponent function already exists,</span> 405 406querystring.<span class="hljs-title function_">stringify</span>({ <span class="hljs-attr">w</span>: <span class="hljs-string">'中文'</span>, <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar'</span> }, <span class="hljs-literal">null</span>, <span class="hljs-literal">null</span>, 407 { <span class="hljs-attr">encodeURIComponent</span>: gbkEncodeURIComponent });</code> <button class="copy-button">copy</button></pre> 408</section><section><h3><code>querystring.unescape(str)</code><span><a class="mark" href="#querystringunescapestr" id="querystringunescapestr">#</a></span><a aria-hidden="true" class="legacy" id="querystring_querystring_unescape_str"></a></h3> 409<div class="api_metadata"> 410<span>Added in: v0.1.25</span> 411</div> 412<ul> 413<li><code>str</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 414</ul> 415<p>The <code>querystring.unescape()</code> method performs decoding of URL percent-encoded 416characters on the given <code>str</code>.</p> 417<p>The <code>querystring.unescape()</code> method is used by <code>querystring.parse()</code> and is 418generally not expected to be used directly. It is exported primarily to allow 419application code to provide a replacement decoding implementation if 420necessary by assigning <code>querystring.unescape</code> to an alternative function.</p> 421<p>By default, the <code>querystring.unescape()</code> method will attempt to use the 422JavaScript built-in <code>decodeURIComponent()</code> method to decode. If that fails, 423a safer equivalent that does not throw on malformed URLs will be used.</p></section> 424 <!-- API END --> 425 </div> 426 </div> 427 </div> 428</body> 429</html> 430