11cb0ef41Sopenharmony_ci# URL 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci<!--introduced_in=v0.10.0--> 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci> Stability: 2 - Stable 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci<!-- source_link=lib/url.js --> 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciThe `node:url` module provides utilities for URL resolution and parsing. It can 101cb0ef41Sopenharmony_cibe accessed using: 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci```mjs 131cb0ef41Sopenharmony_ciimport url from 'node:url'; 141cb0ef41Sopenharmony_ci``` 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci```cjs 171cb0ef41Sopenharmony_ciconst url = require('node:url'); 181cb0ef41Sopenharmony_ci``` 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci## URL strings and URL objects 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciA URL string is a structured string containing multiple meaningful components. 231cb0ef41Sopenharmony_ciWhen parsed, a URL object is returned containing properties for each of these 241cb0ef41Sopenharmony_cicomponents. 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ciThe `node:url` module provides two APIs for working with URLs: a legacy API that 271cb0ef41Sopenharmony_ciis Node.js specific, and a newer API that implements the same 281cb0ef41Sopenharmony_ci[WHATWG URL Standard][] used by web browsers. 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciA comparison between the WHATWG and legacy APIs is provided below. Above the URL 311cb0ef41Sopenharmony_ci`'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'`, properties 321cb0ef41Sopenharmony_ciof an object returned by the legacy `url.parse()` are shown. Below it are 331cb0ef41Sopenharmony_ciproperties of a WHATWG `URL` object. 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ciWHATWG URL's `origin` property includes `protocol` and `host`, but not 361cb0ef41Sopenharmony_ci`username` or `password`. 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci```text 391cb0ef41Sopenharmony_ci┌────────────────────────────────────────────────────────────────────────────────────────────────┐ 401cb0ef41Sopenharmony_ci│ href │ 411cb0ef41Sopenharmony_ci├──────────┬──┬─────────────────────┬────────────────────────┬───────────────────────────┬───────┤ 421cb0ef41Sopenharmony_ci│ protocol │ │ auth │ host │ path │ hash │ 431cb0ef41Sopenharmony_ci│ │ │ ├─────────────────┬──────┼──────────┬────────────────┤ │ 441cb0ef41Sopenharmony_ci│ │ │ │ hostname │ port │ pathname │ search │ │ 451cb0ef41Sopenharmony_ci│ │ │ │ │ │ ├─┬──────────────┤ │ 461cb0ef41Sopenharmony_ci│ │ │ │ │ │ │ │ query │ │ 471cb0ef41Sopenharmony_ci" https: // user : pass @ sub.example.com : 8080 /p/a/t/h ? query=string #hash " 481cb0ef41Sopenharmony_ci│ │ │ │ │ hostname │ port │ │ │ │ 491cb0ef41Sopenharmony_ci│ │ │ │ ├─────────────────┴──────┤ │ │ │ 501cb0ef41Sopenharmony_ci│ protocol │ │ username │ password │ host │ │ │ │ 511cb0ef41Sopenharmony_ci├──────────┴──┼──────────┴──────────┼────────────────────────┤ │ │ │ 521cb0ef41Sopenharmony_ci│ origin │ │ origin │ pathname │ search │ hash │ 531cb0ef41Sopenharmony_ci├─────────────┴─────────────────────┴────────────────────────┴──────────┴────────────────┴───────┤ 541cb0ef41Sopenharmony_ci│ href │ 551cb0ef41Sopenharmony_ci└────────────────────────────────────────────────────────────────────────────────────────────────┘ 561cb0ef41Sopenharmony_ci(All spaces in the "" line should be ignored. They are purely for formatting.) 571cb0ef41Sopenharmony_ci``` 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ciParsing the URL string using the WHATWG API: 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci```js 621cb0ef41Sopenharmony_ciconst myURL = 631cb0ef41Sopenharmony_ci new URL('https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'); 641cb0ef41Sopenharmony_ci``` 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ciParsing the URL string using the legacy API: 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci```mjs 691cb0ef41Sopenharmony_ciimport url from 'node:url'; 701cb0ef41Sopenharmony_ciconst myURL = 711cb0ef41Sopenharmony_ci url.parse('https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'); 721cb0ef41Sopenharmony_ci``` 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci```cjs 751cb0ef41Sopenharmony_ciconst url = require('node:url'); 761cb0ef41Sopenharmony_ciconst myURL = 771cb0ef41Sopenharmony_ci url.parse('https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'); 781cb0ef41Sopenharmony_ci``` 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci### Constructing a URL from component parts and getting the constructed string 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ciIt is possible to construct a WHATWG URL from component parts using either the 831cb0ef41Sopenharmony_ciproperty setters or a template literal string: 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci```js 861cb0ef41Sopenharmony_ciconst myURL = new URL('https://example.org'); 871cb0ef41Sopenharmony_cimyURL.pathname = '/a/b/c'; 881cb0ef41Sopenharmony_cimyURL.search = '?d=e'; 891cb0ef41Sopenharmony_cimyURL.hash = '#fgh'; 901cb0ef41Sopenharmony_ci``` 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci```js 931cb0ef41Sopenharmony_ciconst pathname = '/a/b/c'; 941cb0ef41Sopenharmony_ciconst search = '?d=e'; 951cb0ef41Sopenharmony_ciconst hash = '#fgh'; 961cb0ef41Sopenharmony_ciconst myURL = new URL(`https://example.org${pathname}${search}${hash}`); 971cb0ef41Sopenharmony_ci``` 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ciTo get the constructed URL string, use the `href` property accessor: 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci```js 1021cb0ef41Sopenharmony_ciconsole.log(myURL.href); 1031cb0ef41Sopenharmony_ci``` 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci## The WHATWG URL API 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci### Class: `URL` 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ci<!-- YAML 1101cb0ef41Sopenharmony_ciadded: 1111cb0ef41Sopenharmony_ci - v7.0.0 1121cb0ef41Sopenharmony_ci - v6.13.0 1131cb0ef41Sopenharmony_cichanges: 1141cb0ef41Sopenharmony_ci - version: v10.0.0 1151cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/18281 1161cb0ef41Sopenharmony_ci description: The class is now available on the global object. 1171cb0ef41Sopenharmony_ci--> 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ciBrowser-compatible `URL` class, implemented by following the WHATWG URL 1201cb0ef41Sopenharmony_ciStandard. [Examples of parsed URLs][] may be found in the Standard itself. 1211cb0ef41Sopenharmony_ciThe `URL` class is also available on the global object. 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ciIn accordance with browser conventions, all properties of `URL` objects 1241cb0ef41Sopenharmony_ciare implemented as getters and setters on the class prototype, rather than as 1251cb0ef41Sopenharmony_cidata properties on the object itself. Thus, unlike [legacy `urlObject`][]s, 1261cb0ef41Sopenharmony_ciusing the `delete` keyword on any properties of `URL` objects (e.g. `delete 1271cb0ef41Sopenharmony_cimyURL.protocol`, `delete myURL.pathname`, etc) has no effect but will still 1281cb0ef41Sopenharmony_cireturn `true`. 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ci#### `new URL(input[, base])` 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ci<!-- 1331cb0ef41Sopenharmony_cichanges: 1341cb0ef41Sopenharmony_ci - version: v18.17.0 1351cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/47339 1361cb0ef41Sopenharmony_ci description: ICU requirement is removed. 1371cb0ef41Sopenharmony_ci--> 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ci* `input` {string} The absolute or relative input URL to parse. If `input` 1401cb0ef41Sopenharmony_ci is relative, then `base` is required. If `input` is absolute, the `base` 1411cb0ef41Sopenharmony_ci is ignored. If `input` is not a string, it is [converted to a string][] first. 1421cb0ef41Sopenharmony_ci* `base` {string} The base URL to resolve against if the `input` is not 1431cb0ef41Sopenharmony_ci absolute. If `base` is not a string, it is [converted to a string][] first. 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_ciCreates a new `URL` object by parsing the `input` relative to the `base`. If 1461cb0ef41Sopenharmony_ci`base` is passed as a string, it will be parsed equivalent to `new URL(base)`. 1471cb0ef41Sopenharmony_ci 1481cb0ef41Sopenharmony_ci```js 1491cb0ef41Sopenharmony_ciconst myURL = new URL('/foo', 'https://example.org/'); 1501cb0ef41Sopenharmony_ci// https://example.org/foo 1511cb0ef41Sopenharmony_ci``` 1521cb0ef41Sopenharmony_ci 1531cb0ef41Sopenharmony_ciThe URL constructor is accessible as a property on the global object. 1541cb0ef41Sopenharmony_ciIt can also be imported from the built-in url module: 1551cb0ef41Sopenharmony_ci 1561cb0ef41Sopenharmony_ci```mjs 1571cb0ef41Sopenharmony_ciimport { URL } from 'node:url'; 1581cb0ef41Sopenharmony_ciconsole.log(URL === globalThis.URL); // Prints 'true'. 1591cb0ef41Sopenharmony_ci``` 1601cb0ef41Sopenharmony_ci 1611cb0ef41Sopenharmony_ci```cjs 1621cb0ef41Sopenharmony_ciconsole.log(URL === require('node:url').URL); // Prints 'true'. 1631cb0ef41Sopenharmony_ci``` 1641cb0ef41Sopenharmony_ci 1651cb0ef41Sopenharmony_ciA `TypeError` will be thrown if the `input` or `base` are not valid URLs. Note 1661cb0ef41Sopenharmony_cithat an effort will be made to coerce the given values into strings. For 1671cb0ef41Sopenharmony_ciinstance: 1681cb0ef41Sopenharmony_ci 1691cb0ef41Sopenharmony_ci```js 1701cb0ef41Sopenharmony_ciconst myURL = new URL({ toString: () => 'https://example.org/' }); 1711cb0ef41Sopenharmony_ci// https://example.org/ 1721cb0ef41Sopenharmony_ci``` 1731cb0ef41Sopenharmony_ci 1741cb0ef41Sopenharmony_ciUnicode characters appearing within the host name of `input` will be 1751cb0ef41Sopenharmony_ciautomatically converted to ASCII using the [Punycode][] algorithm. 1761cb0ef41Sopenharmony_ci 1771cb0ef41Sopenharmony_ci```js 1781cb0ef41Sopenharmony_ciconst myURL = new URL('https://測試'); 1791cb0ef41Sopenharmony_ci// https://xn--g6w251d/ 1801cb0ef41Sopenharmony_ci``` 1811cb0ef41Sopenharmony_ci 1821cb0ef41Sopenharmony_ciIn cases where it is not known in advance if `input` is an absolute URL 1831cb0ef41Sopenharmony_ciand a `base` is provided, it is advised to validate that the `origin` of 1841cb0ef41Sopenharmony_cithe `URL` object is what is expected. 1851cb0ef41Sopenharmony_ci 1861cb0ef41Sopenharmony_ci```js 1871cb0ef41Sopenharmony_cilet myURL = new URL('http://Example.com/', 'https://example.org/'); 1881cb0ef41Sopenharmony_ci// http://example.com/ 1891cb0ef41Sopenharmony_ci 1901cb0ef41Sopenharmony_cimyURL = new URL('https://Example.com/', 'https://example.org/'); 1911cb0ef41Sopenharmony_ci// https://example.com/ 1921cb0ef41Sopenharmony_ci 1931cb0ef41Sopenharmony_cimyURL = new URL('foo://Example.com/', 'https://example.org/'); 1941cb0ef41Sopenharmony_ci// foo://Example.com/ 1951cb0ef41Sopenharmony_ci 1961cb0ef41Sopenharmony_cimyURL = new URL('http:Example.com/', 'https://example.org/'); 1971cb0ef41Sopenharmony_ci// http://example.com/ 1981cb0ef41Sopenharmony_ci 1991cb0ef41Sopenharmony_cimyURL = new URL('https:Example.com/', 'https://example.org/'); 2001cb0ef41Sopenharmony_ci// https://example.org/Example.com/ 2011cb0ef41Sopenharmony_ci 2021cb0ef41Sopenharmony_cimyURL = new URL('foo:Example.com/', 'https://example.org/'); 2031cb0ef41Sopenharmony_ci// foo:Example.com/ 2041cb0ef41Sopenharmony_ci``` 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ci#### `url.hash` 2071cb0ef41Sopenharmony_ci 2081cb0ef41Sopenharmony_ci* {string} 2091cb0ef41Sopenharmony_ci 2101cb0ef41Sopenharmony_ciGets and sets the fragment portion of the URL. 2111cb0ef41Sopenharmony_ci 2121cb0ef41Sopenharmony_ci```js 2131cb0ef41Sopenharmony_ciconst myURL = new URL('https://example.org/foo#bar'); 2141cb0ef41Sopenharmony_ciconsole.log(myURL.hash); 2151cb0ef41Sopenharmony_ci// Prints #bar 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_cimyURL.hash = 'baz'; 2181cb0ef41Sopenharmony_ciconsole.log(myURL.href); 2191cb0ef41Sopenharmony_ci// Prints https://example.org/foo#baz 2201cb0ef41Sopenharmony_ci``` 2211cb0ef41Sopenharmony_ci 2221cb0ef41Sopenharmony_ciInvalid URL characters included in the value assigned to the `hash` property 2231cb0ef41Sopenharmony_ciare [percent-encoded][]. The selection of which characters to 2241cb0ef41Sopenharmony_cipercent-encode may vary somewhat from what the [`url.parse()`][] and 2251cb0ef41Sopenharmony_ci[`url.format()`][] methods would produce. 2261cb0ef41Sopenharmony_ci 2271cb0ef41Sopenharmony_ci#### `url.host` 2281cb0ef41Sopenharmony_ci 2291cb0ef41Sopenharmony_ci* {string} 2301cb0ef41Sopenharmony_ci 2311cb0ef41Sopenharmony_ciGets and sets the host portion of the URL. 2321cb0ef41Sopenharmony_ci 2331cb0ef41Sopenharmony_ci```js 2341cb0ef41Sopenharmony_ciconst myURL = new URL('https://example.org:81/foo'); 2351cb0ef41Sopenharmony_ciconsole.log(myURL.host); 2361cb0ef41Sopenharmony_ci// Prints example.org:81 2371cb0ef41Sopenharmony_ci 2381cb0ef41Sopenharmony_cimyURL.host = 'example.com:82'; 2391cb0ef41Sopenharmony_ciconsole.log(myURL.href); 2401cb0ef41Sopenharmony_ci// Prints https://example.com:82/foo 2411cb0ef41Sopenharmony_ci``` 2421cb0ef41Sopenharmony_ci 2431cb0ef41Sopenharmony_ciInvalid host values assigned to the `host` property are ignored. 2441cb0ef41Sopenharmony_ci 2451cb0ef41Sopenharmony_ci#### `url.hostname` 2461cb0ef41Sopenharmony_ci 2471cb0ef41Sopenharmony_ci* {string} 2481cb0ef41Sopenharmony_ci 2491cb0ef41Sopenharmony_ciGets and sets the host name portion of the URL. The key difference between 2501cb0ef41Sopenharmony_ci`url.host` and `url.hostname` is that `url.hostname` does _not_ include the 2511cb0ef41Sopenharmony_ciport. 2521cb0ef41Sopenharmony_ci 2531cb0ef41Sopenharmony_ci```js 2541cb0ef41Sopenharmony_ciconst myURL = new URL('https://example.org:81/foo'); 2551cb0ef41Sopenharmony_ciconsole.log(myURL.hostname); 2561cb0ef41Sopenharmony_ci// Prints example.org 2571cb0ef41Sopenharmony_ci 2581cb0ef41Sopenharmony_ci// Setting the hostname does not change the port 2591cb0ef41Sopenharmony_cimyURL.hostname = 'example.com'; 2601cb0ef41Sopenharmony_ciconsole.log(myURL.href); 2611cb0ef41Sopenharmony_ci// Prints https://example.com:81/foo 2621cb0ef41Sopenharmony_ci 2631cb0ef41Sopenharmony_ci// Use myURL.host to change the hostname and port 2641cb0ef41Sopenharmony_cimyURL.host = 'example.org:82'; 2651cb0ef41Sopenharmony_ciconsole.log(myURL.href); 2661cb0ef41Sopenharmony_ci// Prints https://example.org:82/foo 2671cb0ef41Sopenharmony_ci``` 2681cb0ef41Sopenharmony_ci 2691cb0ef41Sopenharmony_ciInvalid host name values assigned to the `hostname` property are ignored. 2701cb0ef41Sopenharmony_ci 2711cb0ef41Sopenharmony_ci#### `url.href` 2721cb0ef41Sopenharmony_ci 2731cb0ef41Sopenharmony_ci* {string} 2741cb0ef41Sopenharmony_ci 2751cb0ef41Sopenharmony_ciGets and sets the serialized URL. 2761cb0ef41Sopenharmony_ci 2771cb0ef41Sopenharmony_ci```js 2781cb0ef41Sopenharmony_ciconst myURL = new URL('https://example.org/foo'); 2791cb0ef41Sopenharmony_ciconsole.log(myURL.href); 2801cb0ef41Sopenharmony_ci// Prints https://example.org/foo 2811cb0ef41Sopenharmony_ci 2821cb0ef41Sopenharmony_cimyURL.href = 'https://example.com/bar'; 2831cb0ef41Sopenharmony_ciconsole.log(myURL.href); 2841cb0ef41Sopenharmony_ci// Prints https://example.com/bar 2851cb0ef41Sopenharmony_ci``` 2861cb0ef41Sopenharmony_ci 2871cb0ef41Sopenharmony_ciGetting the value of the `href` property is equivalent to calling 2881cb0ef41Sopenharmony_ci[`url.toString()`][]. 2891cb0ef41Sopenharmony_ci 2901cb0ef41Sopenharmony_ciSetting the value of this property to a new value is equivalent to creating a 2911cb0ef41Sopenharmony_cinew `URL` object using [`new URL(value)`][`new URL()`]. Each of the `URL` 2921cb0ef41Sopenharmony_ciobject's properties will be modified. 2931cb0ef41Sopenharmony_ci 2941cb0ef41Sopenharmony_ciIf the value assigned to the `href` property is not a valid URL, a `TypeError` 2951cb0ef41Sopenharmony_ciwill be thrown. 2961cb0ef41Sopenharmony_ci 2971cb0ef41Sopenharmony_ci#### `url.origin` 2981cb0ef41Sopenharmony_ci 2991cb0ef41Sopenharmony_ci<!-- YAML 3001cb0ef41Sopenharmony_cichanges: 3011cb0ef41Sopenharmony_ci - version: v15.0.0 3021cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/33325 3031cb0ef41Sopenharmony_ci description: The scheme "gopher" is no longer special and `url.origin` now 3041cb0ef41Sopenharmony_ci returns `'null'` for it. 3051cb0ef41Sopenharmony_ci--> 3061cb0ef41Sopenharmony_ci 3071cb0ef41Sopenharmony_ci* {string} 3081cb0ef41Sopenharmony_ci 3091cb0ef41Sopenharmony_ciGets the read-only serialization of the URL's origin. 3101cb0ef41Sopenharmony_ci 3111cb0ef41Sopenharmony_ci```js 3121cb0ef41Sopenharmony_ciconst myURL = new URL('https://example.org/foo/bar?baz'); 3131cb0ef41Sopenharmony_ciconsole.log(myURL.origin); 3141cb0ef41Sopenharmony_ci// Prints https://example.org 3151cb0ef41Sopenharmony_ci``` 3161cb0ef41Sopenharmony_ci 3171cb0ef41Sopenharmony_ci```js 3181cb0ef41Sopenharmony_ciconst idnURL = new URL('https://測試'); 3191cb0ef41Sopenharmony_ciconsole.log(idnURL.origin); 3201cb0ef41Sopenharmony_ci// Prints https://xn--g6w251d 3211cb0ef41Sopenharmony_ci 3221cb0ef41Sopenharmony_ciconsole.log(idnURL.hostname); 3231cb0ef41Sopenharmony_ci// Prints xn--g6w251d 3241cb0ef41Sopenharmony_ci``` 3251cb0ef41Sopenharmony_ci 3261cb0ef41Sopenharmony_ci#### `url.password` 3271cb0ef41Sopenharmony_ci 3281cb0ef41Sopenharmony_ci* {string} 3291cb0ef41Sopenharmony_ci 3301cb0ef41Sopenharmony_ciGets and sets the password portion of the URL. 3311cb0ef41Sopenharmony_ci 3321cb0ef41Sopenharmony_ci```js 3331cb0ef41Sopenharmony_ciconst myURL = new URL('https://abc:xyz@example.com'); 3341cb0ef41Sopenharmony_ciconsole.log(myURL.password); 3351cb0ef41Sopenharmony_ci// Prints xyz 3361cb0ef41Sopenharmony_ci 3371cb0ef41Sopenharmony_cimyURL.password = '123'; 3381cb0ef41Sopenharmony_ciconsole.log(myURL.href); 3391cb0ef41Sopenharmony_ci// Prints https://abc:123@example.com/ 3401cb0ef41Sopenharmony_ci``` 3411cb0ef41Sopenharmony_ci 3421cb0ef41Sopenharmony_ciInvalid URL characters included in the value assigned to the `password` property 3431cb0ef41Sopenharmony_ciare [percent-encoded][]. The selection of which characters to 3441cb0ef41Sopenharmony_cipercent-encode may vary somewhat from what the [`url.parse()`][] and 3451cb0ef41Sopenharmony_ci[`url.format()`][] methods would produce. 3461cb0ef41Sopenharmony_ci 3471cb0ef41Sopenharmony_ci#### `url.pathname` 3481cb0ef41Sopenharmony_ci 3491cb0ef41Sopenharmony_ci* {string} 3501cb0ef41Sopenharmony_ci 3511cb0ef41Sopenharmony_ciGets and sets the path portion of the URL. 3521cb0ef41Sopenharmony_ci 3531cb0ef41Sopenharmony_ci```js 3541cb0ef41Sopenharmony_ciconst myURL = new URL('https://example.org/abc/xyz?123'); 3551cb0ef41Sopenharmony_ciconsole.log(myURL.pathname); 3561cb0ef41Sopenharmony_ci// Prints /abc/xyz 3571cb0ef41Sopenharmony_ci 3581cb0ef41Sopenharmony_cimyURL.pathname = '/abcdef'; 3591cb0ef41Sopenharmony_ciconsole.log(myURL.href); 3601cb0ef41Sopenharmony_ci// Prints https://example.org/abcdef?123 3611cb0ef41Sopenharmony_ci``` 3621cb0ef41Sopenharmony_ci 3631cb0ef41Sopenharmony_ciInvalid URL characters included in the value assigned to the `pathname` 3641cb0ef41Sopenharmony_ciproperty are [percent-encoded][]. The selection of which characters 3651cb0ef41Sopenharmony_cito percent-encode may vary somewhat from what the [`url.parse()`][] and 3661cb0ef41Sopenharmony_ci[`url.format()`][] methods would produce. 3671cb0ef41Sopenharmony_ci 3681cb0ef41Sopenharmony_ci#### `url.port` 3691cb0ef41Sopenharmony_ci 3701cb0ef41Sopenharmony_ci<!-- YAML 3711cb0ef41Sopenharmony_cichanges: 3721cb0ef41Sopenharmony_ci - version: v15.0.0 3731cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/33325 3741cb0ef41Sopenharmony_ci description: The scheme "gopher" is no longer special. 3751cb0ef41Sopenharmony_ci--> 3761cb0ef41Sopenharmony_ci 3771cb0ef41Sopenharmony_ci* {string} 3781cb0ef41Sopenharmony_ci 3791cb0ef41Sopenharmony_ciGets and sets the port portion of the URL. 3801cb0ef41Sopenharmony_ci 3811cb0ef41Sopenharmony_ciThe port value may be a number or a string containing a number in the range 3821cb0ef41Sopenharmony_ci`0` to `65535` (inclusive). Setting the value to the default port of the 3831cb0ef41Sopenharmony_ci`URL` objects given `protocol` will result in the `port` value becoming 3841cb0ef41Sopenharmony_cithe empty string (`''`). 3851cb0ef41Sopenharmony_ci 3861cb0ef41Sopenharmony_ciThe port value can be an empty string in which case the port depends on 3871cb0ef41Sopenharmony_cithe protocol/scheme: 3881cb0ef41Sopenharmony_ci 3891cb0ef41Sopenharmony_ci| protocol | port | 3901cb0ef41Sopenharmony_ci| -------- | ---- | 3911cb0ef41Sopenharmony_ci| "ftp" | 21 | 3921cb0ef41Sopenharmony_ci| "file" | | 3931cb0ef41Sopenharmony_ci| "http" | 80 | 3941cb0ef41Sopenharmony_ci| "https" | 443 | 3951cb0ef41Sopenharmony_ci| "ws" | 80 | 3961cb0ef41Sopenharmony_ci| "wss" | 443 | 3971cb0ef41Sopenharmony_ci 3981cb0ef41Sopenharmony_ciUpon assigning a value to the port, the value will first be converted to a 3991cb0ef41Sopenharmony_cistring using `.toString()`. 4001cb0ef41Sopenharmony_ci 4011cb0ef41Sopenharmony_ciIf that string is invalid but it begins with a number, the leading number is 4021cb0ef41Sopenharmony_ciassigned to `port`. 4031cb0ef41Sopenharmony_ciIf the number lies outside the range denoted above, it is ignored. 4041cb0ef41Sopenharmony_ci 4051cb0ef41Sopenharmony_ci```js 4061cb0ef41Sopenharmony_ciconst myURL = new URL('https://example.org:8888'); 4071cb0ef41Sopenharmony_ciconsole.log(myURL.port); 4081cb0ef41Sopenharmony_ci// Prints 8888 4091cb0ef41Sopenharmony_ci 4101cb0ef41Sopenharmony_ci// Default ports are automatically transformed to the empty string 4111cb0ef41Sopenharmony_ci// (HTTPS protocol's default port is 443) 4121cb0ef41Sopenharmony_cimyURL.port = '443'; 4131cb0ef41Sopenharmony_ciconsole.log(myURL.port); 4141cb0ef41Sopenharmony_ci// Prints the empty string 4151cb0ef41Sopenharmony_ciconsole.log(myURL.href); 4161cb0ef41Sopenharmony_ci// Prints https://example.org/ 4171cb0ef41Sopenharmony_ci 4181cb0ef41Sopenharmony_cimyURL.port = 1234; 4191cb0ef41Sopenharmony_ciconsole.log(myURL.port); 4201cb0ef41Sopenharmony_ci// Prints 1234 4211cb0ef41Sopenharmony_ciconsole.log(myURL.href); 4221cb0ef41Sopenharmony_ci// Prints https://example.org:1234/ 4231cb0ef41Sopenharmony_ci 4241cb0ef41Sopenharmony_ci// Completely invalid port strings are ignored 4251cb0ef41Sopenharmony_cimyURL.port = 'abcd'; 4261cb0ef41Sopenharmony_ciconsole.log(myURL.port); 4271cb0ef41Sopenharmony_ci// Prints 1234 4281cb0ef41Sopenharmony_ci 4291cb0ef41Sopenharmony_ci// Leading numbers are treated as a port number 4301cb0ef41Sopenharmony_cimyURL.port = '5678abcd'; 4311cb0ef41Sopenharmony_ciconsole.log(myURL.port); 4321cb0ef41Sopenharmony_ci// Prints 5678 4331cb0ef41Sopenharmony_ci 4341cb0ef41Sopenharmony_ci// Non-integers are truncated 4351cb0ef41Sopenharmony_cimyURL.port = 1234.5678; 4361cb0ef41Sopenharmony_ciconsole.log(myURL.port); 4371cb0ef41Sopenharmony_ci// Prints 1234 4381cb0ef41Sopenharmony_ci 4391cb0ef41Sopenharmony_ci// Out-of-range numbers which are not represented in scientific notation 4401cb0ef41Sopenharmony_ci// will be ignored. 4411cb0ef41Sopenharmony_cimyURL.port = 1e10; // 10000000000, will be range-checked as described below 4421cb0ef41Sopenharmony_ciconsole.log(myURL.port); 4431cb0ef41Sopenharmony_ci// Prints 1234 4441cb0ef41Sopenharmony_ci``` 4451cb0ef41Sopenharmony_ci 4461cb0ef41Sopenharmony_ciNumbers which contain a decimal point, 4471cb0ef41Sopenharmony_cisuch as floating-point numbers or numbers in scientific notation, 4481cb0ef41Sopenharmony_ciare not an exception to this rule. 4491cb0ef41Sopenharmony_ciLeading numbers up to the decimal point will be set as the URL's port, 4501cb0ef41Sopenharmony_ciassuming they are valid: 4511cb0ef41Sopenharmony_ci 4521cb0ef41Sopenharmony_ci```js 4531cb0ef41Sopenharmony_cimyURL.port = 4.567e21; 4541cb0ef41Sopenharmony_ciconsole.log(myURL.port); 4551cb0ef41Sopenharmony_ci// Prints 4 (because it is the leading number in the string '4.567e21') 4561cb0ef41Sopenharmony_ci``` 4571cb0ef41Sopenharmony_ci 4581cb0ef41Sopenharmony_ci#### `url.protocol` 4591cb0ef41Sopenharmony_ci 4601cb0ef41Sopenharmony_ci* {string} 4611cb0ef41Sopenharmony_ci 4621cb0ef41Sopenharmony_ciGets and sets the protocol portion of the URL. 4631cb0ef41Sopenharmony_ci 4641cb0ef41Sopenharmony_ci```js 4651cb0ef41Sopenharmony_ciconst myURL = new URL('https://example.org'); 4661cb0ef41Sopenharmony_ciconsole.log(myURL.protocol); 4671cb0ef41Sopenharmony_ci// Prints https: 4681cb0ef41Sopenharmony_ci 4691cb0ef41Sopenharmony_cimyURL.protocol = 'ftp'; 4701cb0ef41Sopenharmony_ciconsole.log(myURL.href); 4711cb0ef41Sopenharmony_ci// Prints ftp://example.org/ 4721cb0ef41Sopenharmony_ci``` 4731cb0ef41Sopenharmony_ci 4741cb0ef41Sopenharmony_ciInvalid URL protocol values assigned to the `protocol` property are ignored. 4751cb0ef41Sopenharmony_ci 4761cb0ef41Sopenharmony_ci##### Special schemes 4771cb0ef41Sopenharmony_ci 4781cb0ef41Sopenharmony_ci<!-- YAML 4791cb0ef41Sopenharmony_cichanges: 4801cb0ef41Sopenharmony_ci - version: v15.0.0 4811cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/33325 4821cb0ef41Sopenharmony_ci description: The scheme "gopher" is no longer special. 4831cb0ef41Sopenharmony_ci--> 4841cb0ef41Sopenharmony_ci 4851cb0ef41Sopenharmony_ciThe [WHATWG URL Standard][] considers a handful of URL protocol schemes to be 4861cb0ef41Sopenharmony_ci_special_ in terms of how they are parsed and serialized. When a URL is 4871cb0ef41Sopenharmony_ciparsed using one of these special protocols, the `url.protocol` property 4881cb0ef41Sopenharmony_cimay be changed to another special protocol but cannot be changed to a 4891cb0ef41Sopenharmony_cinon-special protocol, and vice versa. 4901cb0ef41Sopenharmony_ci 4911cb0ef41Sopenharmony_ciFor instance, changing from `http` to `https` works: 4921cb0ef41Sopenharmony_ci 4931cb0ef41Sopenharmony_ci```js 4941cb0ef41Sopenharmony_ciconst u = new URL('http://example.org'); 4951cb0ef41Sopenharmony_ciu.protocol = 'https'; 4961cb0ef41Sopenharmony_ciconsole.log(u.href); 4971cb0ef41Sopenharmony_ci// https://example.org/ 4981cb0ef41Sopenharmony_ci``` 4991cb0ef41Sopenharmony_ci 5001cb0ef41Sopenharmony_ciHowever, changing from `http` to a hypothetical `fish` protocol does not 5011cb0ef41Sopenharmony_cibecause the new protocol is not special. 5021cb0ef41Sopenharmony_ci 5031cb0ef41Sopenharmony_ci```js 5041cb0ef41Sopenharmony_ciconst u = new URL('http://example.org'); 5051cb0ef41Sopenharmony_ciu.protocol = 'fish'; 5061cb0ef41Sopenharmony_ciconsole.log(u.href); 5071cb0ef41Sopenharmony_ci// http://example.org/ 5081cb0ef41Sopenharmony_ci``` 5091cb0ef41Sopenharmony_ci 5101cb0ef41Sopenharmony_ciLikewise, changing from a non-special protocol to a special protocol is also 5111cb0ef41Sopenharmony_cinot permitted: 5121cb0ef41Sopenharmony_ci 5131cb0ef41Sopenharmony_ci```js 5141cb0ef41Sopenharmony_ciconst u = new URL('fish://example.org'); 5151cb0ef41Sopenharmony_ciu.protocol = 'http'; 5161cb0ef41Sopenharmony_ciconsole.log(u.href); 5171cb0ef41Sopenharmony_ci// fish://example.org 5181cb0ef41Sopenharmony_ci``` 5191cb0ef41Sopenharmony_ci 5201cb0ef41Sopenharmony_ciAccording to the WHATWG URL Standard, special protocol schemes are `ftp`, 5211cb0ef41Sopenharmony_ci`file`, `http`, `https`, `ws`, and `wss`. 5221cb0ef41Sopenharmony_ci 5231cb0ef41Sopenharmony_ci#### `url.search` 5241cb0ef41Sopenharmony_ci 5251cb0ef41Sopenharmony_ci* {string} 5261cb0ef41Sopenharmony_ci 5271cb0ef41Sopenharmony_ciGets and sets the serialized query portion of the URL. 5281cb0ef41Sopenharmony_ci 5291cb0ef41Sopenharmony_ci```js 5301cb0ef41Sopenharmony_ciconst myURL = new URL('https://example.org/abc?123'); 5311cb0ef41Sopenharmony_ciconsole.log(myURL.search); 5321cb0ef41Sopenharmony_ci// Prints ?123 5331cb0ef41Sopenharmony_ci 5341cb0ef41Sopenharmony_cimyURL.search = 'abc=xyz'; 5351cb0ef41Sopenharmony_ciconsole.log(myURL.href); 5361cb0ef41Sopenharmony_ci// Prints https://example.org/abc?abc=xyz 5371cb0ef41Sopenharmony_ci``` 5381cb0ef41Sopenharmony_ci 5391cb0ef41Sopenharmony_ciAny invalid URL characters appearing in the value assigned the `search` 5401cb0ef41Sopenharmony_ciproperty will be [percent-encoded][]. The selection of which 5411cb0ef41Sopenharmony_cicharacters to percent-encode may vary somewhat from what the [`url.parse()`][] 5421cb0ef41Sopenharmony_ciand [`url.format()`][] methods would produce. 5431cb0ef41Sopenharmony_ci 5441cb0ef41Sopenharmony_ci#### `url.searchParams` 5451cb0ef41Sopenharmony_ci 5461cb0ef41Sopenharmony_ci* {URLSearchParams} 5471cb0ef41Sopenharmony_ci 5481cb0ef41Sopenharmony_ciGets the [`URLSearchParams`][] object representing the query parameters of the 5491cb0ef41Sopenharmony_ciURL. This property is read-only but the `URLSearchParams` object it provides 5501cb0ef41Sopenharmony_cican be used to mutate the URL instance; to replace the entirety of query 5511cb0ef41Sopenharmony_ciparameters of the URL, use the [`url.search`][] setter. See 5521cb0ef41Sopenharmony_ci[`URLSearchParams`][] documentation for details. 5531cb0ef41Sopenharmony_ci 5541cb0ef41Sopenharmony_ciUse care when using `.searchParams` to modify the `URL` because, 5551cb0ef41Sopenharmony_ciper the WHATWG specification, the `URLSearchParams` object uses 5561cb0ef41Sopenharmony_cidifferent rules to determine which characters to percent-encode. For 5571cb0ef41Sopenharmony_ciinstance, the `URL` object will not percent encode the ASCII tilde (`~`) 5581cb0ef41Sopenharmony_cicharacter, while `URLSearchParams` will always encode it: 5591cb0ef41Sopenharmony_ci 5601cb0ef41Sopenharmony_ci```js 5611cb0ef41Sopenharmony_ciconst myURL = new URL('https://example.org/abc?foo=~bar'); 5621cb0ef41Sopenharmony_ci 5631cb0ef41Sopenharmony_ciconsole.log(myURL.search); // prints ?foo=~bar 5641cb0ef41Sopenharmony_ci 5651cb0ef41Sopenharmony_ci// Modify the URL via searchParams... 5661cb0ef41Sopenharmony_cimyURL.searchParams.sort(); 5671cb0ef41Sopenharmony_ci 5681cb0ef41Sopenharmony_ciconsole.log(myURL.search); // prints ?foo=%7Ebar 5691cb0ef41Sopenharmony_ci``` 5701cb0ef41Sopenharmony_ci 5711cb0ef41Sopenharmony_ci#### `url.username` 5721cb0ef41Sopenharmony_ci 5731cb0ef41Sopenharmony_ci* {string} 5741cb0ef41Sopenharmony_ci 5751cb0ef41Sopenharmony_ciGets and sets the username portion of the URL. 5761cb0ef41Sopenharmony_ci 5771cb0ef41Sopenharmony_ci```js 5781cb0ef41Sopenharmony_ciconst myURL = new URL('https://abc:xyz@example.com'); 5791cb0ef41Sopenharmony_ciconsole.log(myURL.username); 5801cb0ef41Sopenharmony_ci// Prints abc 5811cb0ef41Sopenharmony_ci 5821cb0ef41Sopenharmony_cimyURL.username = '123'; 5831cb0ef41Sopenharmony_ciconsole.log(myURL.href); 5841cb0ef41Sopenharmony_ci// Prints https://123:xyz@example.com/ 5851cb0ef41Sopenharmony_ci``` 5861cb0ef41Sopenharmony_ci 5871cb0ef41Sopenharmony_ciAny invalid URL characters appearing in the value assigned the `username` 5881cb0ef41Sopenharmony_ciproperty will be [percent-encoded][]. The selection of which 5891cb0ef41Sopenharmony_cicharacters to percent-encode may vary somewhat from what the [`url.parse()`][] 5901cb0ef41Sopenharmony_ciand [`url.format()`][] methods would produce. 5911cb0ef41Sopenharmony_ci 5921cb0ef41Sopenharmony_ci#### `url.toString()` 5931cb0ef41Sopenharmony_ci 5941cb0ef41Sopenharmony_ci* Returns: {string} 5951cb0ef41Sopenharmony_ci 5961cb0ef41Sopenharmony_ciThe `toString()` method on the `URL` object returns the serialized URL. The 5971cb0ef41Sopenharmony_civalue returned is equivalent to that of [`url.href`][] and [`url.toJSON()`][]. 5981cb0ef41Sopenharmony_ci 5991cb0ef41Sopenharmony_ci#### `url.toJSON()` 6001cb0ef41Sopenharmony_ci 6011cb0ef41Sopenharmony_ci* Returns: {string} 6021cb0ef41Sopenharmony_ci 6031cb0ef41Sopenharmony_ciThe `toJSON()` method on the `URL` object returns the serialized URL. The 6041cb0ef41Sopenharmony_civalue returned is equivalent to that of [`url.href`][] and 6051cb0ef41Sopenharmony_ci[`url.toString()`][]. 6061cb0ef41Sopenharmony_ci 6071cb0ef41Sopenharmony_ciThis method is automatically called when an `URL` object is serialized 6081cb0ef41Sopenharmony_ciwith [`JSON.stringify()`][]. 6091cb0ef41Sopenharmony_ci 6101cb0ef41Sopenharmony_ci```js 6111cb0ef41Sopenharmony_ciconst myURLs = [ 6121cb0ef41Sopenharmony_ci new URL('https://www.example.com'), 6131cb0ef41Sopenharmony_ci new URL('https://test.example.org'), 6141cb0ef41Sopenharmony_ci]; 6151cb0ef41Sopenharmony_ciconsole.log(JSON.stringify(myURLs)); 6161cb0ef41Sopenharmony_ci// Prints ["https://www.example.com/","https://test.example.org/"] 6171cb0ef41Sopenharmony_ci``` 6181cb0ef41Sopenharmony_ci 6191cb0ef41Sopenharmony_ci#### `URL.createObjectURL(blob)` 6201cb0ef41Sopenharmony_ci 6211cb0ef41Sopenharmony_ci<!-- YAML 6221cb0ef41Sopenharmony_ciadded: v16.7.0 6231cb0ef41Sopenharmony_ci--> 6241cb0ef41Sopenharmony_ci 6251cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 6261cb0ef41Sopenharmony_ci 6271cb0ef41Sopenharmony_ci* `blob` {Blob} 6281cb0ef41Sopenharmony_ci* Returns: {string} 6291cb0ef41Sopenharmony_ci 6301cb0ef41Sopenharmony_ciCreates a `'blob:nodedata:...'` URL string that represents the given {Blob} 6311cb0ef41Sopenharmony_ciobject and can be used to retrieve the `Blob` later. 6321cb0ef41Sopenharmony_ci 6331cb0ef41Sopenharmony_ci```js 6341cb0ef41Sopenharmony_ciconst { 6351cb0ef41Sopenharmony_ci Blob, 6361cb0ef41Sopenharmony_ci resolveObjectURL, 6371cb0ef41Sopenharmony_ci} = require('node:buffer'); 6381cb0ef41Sopenharmony_ci 6391cb0ef41Sopenharmony_ciconst blob = new Blob(['hello']); 6401cb0ef41Sopenharmony_ciconst id = URL.createObjectURL(blob); 6411cb0ef41Sopenharmony_ci 6421cb0ef41Sopenharmony_ci// later... 6431cb0ef41Sopenharmony_ci 6441cb0ef41Sopenharmony_ciconst otherBlob = resolveObjectURL(id); 6451cb0ef41Sopenharmony_ciconsole.log(otherBlob.size); 6461cb0ef41Sopenharmony_ci``` 6471cb0ef41Sopenharmony_ci 6481cb0ef41Sopenharmony_ciThe data stored by the registered {Blob} will be retained in memory until 6491cb0ef41Sopenharmony_ci`URL.revokeObjectURL()` is called to remove it. 6501cb0ef41Sopenharmony_ci 6511cb0ef41Sopenharmony_ci`Blob` objects are registered within the current thread. If using Worker 6521cb0ef41Sopenharmony_ciThreads, `Blob` objects registered within one Worker will not be available 6531cb0ef41Sopenharmony_cito other workers or the main thread. 6541cb0ef41Sopenharmony_ci 6551cb0ef41Sopenharmony_ci#### `URL.revokeObjectURL(id)` 6561cb0ef41Sopenharmony_ci 6571cb0ef41Sopenharmony_ci<!-- YAML 6581cb0ef41Sopenharmony_ciadded: v16.7.0 6591cb0ef41Sopenharmony_ci--> 6601cb0ef41Sopenharmony_ci 6611cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 6621cb0ef41Sopenharmony_ci 6631cb0ef41Sopenharmony_ci* `id` {string} A `'blob:nodedata:...` URL string returned by a prior call to 6641cb0ef41Sopenharmony_ci `URL.createObjectURL()`. 6651cb0ef41Sopenharmony_ci 6661cb0ef41Sopenharmony_ciRemoves the stored {Blob} identified by the given ID. Attempting to revoke a 6671cb0ef41Sopenharmony_ciID that isn't registered will silently fail. 6681cb0ef41Sopenharmony_ci 6691cb0ef41Sopenharmony_ci#### `URL.canParse(input[, base])` 6701cb0ef41Sopenharmony_ci 6711cb0ef41Sopenharmony_ci<!-- YAML 6721cb0ef41Sopenharmony_ciadded: v18.17.0 6731cb0ef41Sopenharmony_ci--> 6741cb0ef41Sopenharmony_ci 6751cb0ef41Sopenharmony_ci* `input` {string} The absolute or relative input URL to parse. If `input` 6761cb0ef41Sopenharmony_ci is relative, then `base` is required. If `input` is absolute, the `base` 6771cb0ef41Sopenharmony_ci is ignored. If `input` is not a string, it is [converted to a string][] first. 6781cb0ef41Sopenharmony_ci* `base` {string} The base URL to resolve against if the `input` is not 6791cb0ef41Sopenharmony_ci absolute. If `base` is not a string, it is [converted to a string][] first. 6801cb0ef41Sopenharmony_ci* Returns: {boolean} 6811cb0ef41Sopenharmony_ci 6821cb0ef41Sopenharmony_ciChecks if an `input` relative to the `base` can be parsed to a `URL`. 6831cb0ef41Sopenharmony_ci 6841cb0ef41Sopenharmony_ci```js 6851cb0ef41Sopenharmony_ciconst isValid = URL.canParse('/foo', 'https://example.org/'); // true 6861cb0ef41Sopenharmony_ci 6871cb0ef41Sopenharmony_ciconst isNotValid = URL.canParse('/foo'); // false 6881cb0ef41Sopenharmony_ci``` 6891cb0ef41Sopenharmony_ci 6901cb0ef41Sopenharmony_ci### Class: `URLSearchParams` 6911cb0ef41Sopenharmony_ci 6921cb0ef41Sopenharmony_ci<!-- YAML 6931cb0ef41Sopenharmony_ciadded: 6941cb0ef41Sopenharmony_ci - v7.5.0 6951cb0ef41Sopenharmony_ci - v6.13.0 6961cb0ef41Sopenharmony_cichanges: 6971cb0ef41Sopenharmony_ci - version: v10.0.0 6981cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/18281 6991cb0ef41Sopenharmony_ci description: The class is now available on the global object. 7001cb0ef41Sopenharmony_ci--> 7011cb0ef41Sopenharmony_ci 7021cb0ef41Sopenharmony_ciThe `URLSearchParams` API provides read and write access to the query of a 7031cb0ef41Sopenharmony_ci`URL`. The `URLSearchParams` class can also be used standalone with one of the 7041cb0ef41Sopenharmony_cifour following constructors. 7051cb0ef41Sopenharmony_ciThe `URLSearchParams` class is also available on the global object. 7061cb0ef41Sopenharmony_ci 7071cb0ef41Sopenharmony_ciThe WHATWG `URLSearchParams` interface and the [`querystring`][] module have 7081cb0ef41Sopenharmony_cisimilar purpose, but the purpose of the [`querystring`][] module is more 7091cb0ef41Sopenharmony_cigeneral, as it allows the customization of delimiter characters (`&` and `=`). 7101cb0ef41Sopenharmony_ciOn the other hand, this API is designed purely for URL query strings. 7111cb0ef41Sopenharmony_ci 7121cb0ef41Sopenharmony_ci```js 7131cb0ef41Sopenharmony_ciconst myURL = new URL('https://example.org/?abc=123'); 7141cb0ef41Sopenharmony_ciconsole.log(myURL.searchParams.get('abc')); 7151cb0ef41Sopenharmony_ci// Prints 123 7161cb0ef41Sopenharmony_ci 7171cb0ef41Sopenharmony_cimyURL.searchParams.append('abc', 'xyz'); 7181cb0ef41Sopenharmony_ciconsole.log(myURL.href); 7191cb0ef41Sopenharmony_ci// Prints https://example.org/?abc=123&abc=xyz 7201cb0ef41Sopenharmony_ci 7211cb0ef41Sopenharmony_cimyURL.searchParams.delete('abc'); 7221cb0ef41Sopenharmony_cimyURL.searchParams.set('a', 'b'); 7231cb0ef41Sopenharmony_ciconsole.log(myURL.href); 7241cb0ef41Sopenharmony_ci// Prints https://example.org/?a=b 7251cb0ef41Sopenharmony_ci 7261cb0ef41Sopenharmony_ciconst newSearchParams = new URLSearchParams(myURL.searchParams); 7271cb0ef41Sopenharmony_ci// The above is equivalent to 7281cb0ef41Sopenharmony_ci// const newSearchParams = new URLSearchParams(myURL.search); 7291cb0ef41Sopenharmony_ci 7301cb0ef41Sopenharmony_cinewSearchParams.append('a', 'c'); 7311cb0ef41Sopenharmony_ciconsole.log(myURL.href); 7321cb0ef41Sopenharmony_ci// Prints https://example.org/?a=b 7331cb0ef41Sopenharmony_ciconsole.log(newSearchParams.toString()); 7341cb0ef41Sopenharmony_ci// Prints a=b&a=c 7351cb0ef41Sopenharmony_ci 7361cb0ef41Sopenharmony_ci// newSearchParams.toString() is implicitly called 7371cb0ef41Sopenharmony_cimyURL.search = newSearchParams; 7381cb0ef41Sopenharmony_ciconsole.log(myURL.href); 7391cb0ef41Sopenharmony_ci// Prints https://example.org/?a=b&a=c 7401cb0ef41Sopenharmony_cinewSearchParams.delete('a'); 7411cb0ef41Sopenharmony_ciconsole.log(myURL.href); 7421cb0ef41Sopenharmony_ci// Prints https://example.org/?a=b&a=c 7431cb0ef41Sopenharmony_ci``` 7441cb0ef41Sopenharmony_ci 7451cb0ef41Sopenharmony_ci#### `new URLSearchParams()` 7461cb0ef41Sopenharmony_ci 7471cb0ef41Sopenharmony_ciInstantiate a new empty `URLSearchParams` object. 7481cb0ef41Sopenharmony_ci 7491cb0ef41Sopenharmony_ci#### `new URLSearchParams(string)` 7501cb0ef41Sopenharmony_ci 7511cb0ef41Sopenharmony_ci* `string` {string} A query string 7521cb0ef41Sopenharmony_ci 7531cb0ef41Sopenharmony_ciParse the `string` as a query string, and use it to instantiate a new 7541cb0ef41Sopenharmony_ci`URLSearchParams` object. A leading `'?'`, if present, is ignored. 7551cb0ef41Sopenharmony_ci 7561cb0ef41Sopenharmony_ci```js 7571cb0ef41Sopenharmony_cilet params; 7581cb0ef41Sopenharmony_ci 7591cb0ef41Sopenharmony_ciparams = new URLSearchParams('user=abc&query=xyz'); 7601cb0ef41Sopenharmony_ciconsole.log(params.get('user')); 7611cb0ef41Sopenharmony_ci// Prints 'abc' 7621cb0ef41Sopenharmony_ciconsole.log(params.toString()); 7631cb0ef41Sopenharmony_ci// Prints 'user=abc&query=xyz' 7641cb0ef41Sopenharmony_ci 7651cb0ef41Sopenharmony_ciparams = new URLSearchParams('?user=abc&query=xyz'); 7661cb0ef41Sopenharmony_ciconsole.log(params.toString()); 7671cb0ef41Sopenharmony_ci// Prints 'user=abc&query=xyz' 7681cb0ef41Sopenharmony_ci``` 7691cb0ef41Sopenharmony_ci 7701cb0ef41Sopenharmony_ci#### `new URLSearchParams(obj)` 7711cb0ef41Sopenharmony_ci 7721cb0ef41Sopenharmony_ci<!-- YAML 7731cb0ef41Sopenharmony_ciadded: 7741cb0ef41Sopenharmony_ci - v7.10.0 7751cb0ef41Sopenharmony_ci - v6.13.0 7761cb0ef41Sopenharmony_ci--> 7771cb0ef41Sopenharmony_ci 7781cb0ef41Sopenharmony_ci* `obj` {Object} An object representing a collection of key-value pairs 7791cb0ef41Sopenharmony_ci 7801cb0ef41Sopenharmony_ciInstantiate a new `URLSearchParams` object with a query hash map. The key and 7811cb0ef41Sopenharmony_civalue of each property of `obj` are always coerced to strings. 7821cb0ef41Sopenharmony_ci 7831cb0ef41Sopenharmony_ciUnlike [`querystring`][] module, duplicate keys in the form of array values are 7841cb0ef41Sopenharmony_cinot allowed. Arrays are stringified using [`array.toString()`][], which simply 7851cb0ef41Sopenharmony_cijoins all array elements with commas. 7861cb0ef41Sopenharmony_ci 7871cb0ef41Sopenharmony_ci```js 7881cb0ef41Sopenharmony_ciconst params = new URLSearchParams({ 7891cb0ef41Sopenharmony_ci user: 'abc', 7901cb0ef41Sopenharmony_ci query: ['first', 'second'], 7911cb0ef41Sopenharmony_ci}); 7921cb0ef41Sopenharmony_ciconsole.log(params.getAll('query')); 7931cb0ef41Sopenharmony_ci// Prints [ 'first,second' ] 7941cb0ef41Sopenharmony_ciconsole.log(params.toString()); 7951cb0ef41Sopenharmony_ci// Prints 'user=abc&query=first%2Csecond' 7961cb0ef41Sopenharmony_ci``` 7971cb0ef41Sopenharmony_ci 7981cb0ef41Sopenharmony_ci#### `new URLSearchParams(iterable)` 7991cb0ef41Sopenharmony_ci 8001cb0ef41Sopenharmony_ci<!-- YAML 8011cb0ef41Sopenharmony_ciadded: 8021cb0ef41Sopenharmony_ci - v7.10.0 8031cb0ef41Sopenharmony_ci - v6.13.0 8041cb0ef41Sopenharmony_ci--> 8051cb0ef41Sopenharmony_ci 8061cb0ef41Sopenharmony_ci* `iterable` {Iterable} An iterable object whose elements are key-value pairs 8071cb0ef41Sopenharmony_ci 8081cb0ef41Sopenharmony_ciInstantiate a new `URLSearchParams` object with an iterable map in a way that 8091cb0ef41Sopenharmony_ciis similar to [`Map`][]'s constructor. `iterable` can be an `Array` or any 8101cb0ef41Sopenharmony_ciiterable object. That means `iterable` can be another `URLSearchParams`, in 8111cb0ef41Sopenharmony_ciwhich case the constructor will simply create a clone of the provided 8121cb0ef41Sopenharmony_ci`URLSearchParams`. Elements of `iterable` are key-value pairs, and can 8131cb0ef41Sopenharmony_cithemselves be any iterable object. 8141cb0ef41Sopenharmony_ci 8151cb0ef41Sopenharmony_ciDuplicate keys are allowed. 8161cb0ef41Sopenharmony_ci 8171cb0ef41Sopenharmony_ci```js 8181cb0ef41Sopenharmony_cilet params; 8191cb0ef41Sopenharmony_ci 8201cb0ef41Sopenharmony_ci// Using an array 8211cb0ef41Sopenharmony_ciparams = new URLSearchParams([ 8221cb0ef41Sopenharmony_ci ['user', 'abc'], 8231cb0ef41Sopenharmony_ci ['query', 'first'], 8241cb0ef41Sopenharmony_ci ['query', 'second'], 8251cb0ef41Sopenharmony_ci]); 8261cb0ef41Sopenharmony_ciconsole.log(params.toString()); 8271cb0ef41Sopenharmony_ci// Prints 'user=abc&query=first&query=second' 8281cb0ef41Sopenharmony_ci 8291cb0ef41Sopenharmony_ci// Using a Map object 8301cb0ef41Sopenharmony_ciconst map = new Map(); 8311cb0ef41Sopenharmony_cimap.set('user', 'abc'); 8321cb0ef41Sopenharmony_cimap.set('query', 'xyz'); 8331cb0ef41Sopenharmony_ciparams = new URLSearchParams(map); 8341cb0ef41Sopenharmony_ciconsole.log(params.toString()); 8351cb0ef41Sopenharmony_ci// Prints 'user=abc&query=xyz' 8361cb0ef41Sopenharmony_ci 8371cb0ef41Sopenharmony_ci// Using a generator function 8381cb0ef41Sopenharmony_cifunction* getQueryPairs() { 8391cb0ef41Sopenharmony_ci yield ['user', 'abc']; 8401cb0ef41Sopenharmony_ci yield ['query', 'first']; 8411cb0ef41Sopenharmony_ci yield ['query', 'second']; 8421cb0ef41Sopenharmony_ci} 8431cb0ef41Sopenharmony_ciparams = new URLSearchParams(getQueryPairs()); 8441cb0ef41Sopenharmony_ciconsole.log(params.toString()); 8451cb0ef41Sopenharmony_ci// Prints 'user=abc&query=first&query=second' 8461cb0ef41Sopenharmony_ci 8471cb0ef41Sopenharmony_ci// Each key-value pair must have exactly two elements 8481cb0ef41Sopenharmony_cinew URLSearchParams([ 8491cb0ef41Sopenharmony_ci ['user', 'abc', 'error'], 8501cb0ef41Sopenharmony_ci]); 8511cb0ef41Sopenharmony_ci// Throws TypeError [ERR_INVALID_TUPLE]: 8521cb0ef41Sopenharmony_ci// Each query pair must be an iterable [name, value] tuple 8531cb0ef41Sopenharmony_ci``` 8541cb0ef41Sopenharmony_ci 8551cb0ef41Sopenharmony_ci#### `urlSearchParams.append(name, value)` 8561cb0ef41Sopenharmony_ci 8571cb0ef41Sopenharmony_ci* `name` {string} 8581cb0ef41Sopenharmony_ci* `value` {string} 8591cb0ef41Sopenharmony_ci 8601cb0ef41Sopenharmony_ciAppend a new name-value pair to the query string. 8611cb0ef41Sopenharmony_ci 8621cb0ef41Sopenharmony_ci#### `urlSearchParams.delete(name[, value])` 8631cb0ef41Sopenharmony_ci 8641cb0ef41Sopenharmony_ci<!-- YAML 8651cb0ef41Sopenharmony_cichanges: 8661cb0ef41Sopenharmony_ci - version: v18.18.0 8671cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/47885 8681cb0ef41Sopenharmony_ci description: Add support for optional `value` argument. 8691cb0ef41Sopenharmony_ci--> 8701cb0ef41Sopenharmony_ci 8711cb0ef41Sopenharmony_ci* `name` {string} 8721cb0ef41Sopenharmony_ci* `value` {string} 8731cb0ef41Sopenharmony_ci 8741cb0ef41Sopenharmony_ciIf `value` is provided, removes all name-value pairs 8751cb0ef41Sopenharmony_ciwhere name is `name` and value is `value`.. 8761cb0ef41Sopenharmony_ci 8771cb0ef41Sopenharmony_ciIf `value` is not provided, removes all name-value pairs whose name is `name`. 8781cb0ef41Sopenharmony_ci 8791cb0ef41Sopenharmony_ci#### `urlSearchParams.entries()` 8801cb0ef41Sopenharmony_ci 8811cb0ef41Sopenharmony_ci* Returns: {Iterator} 8821cb0ef41Sopenharmony_ci 8831cb0ef41Sopenharmony_ciReturns an ES6 `Iterator` over each of the name-value pairs in the query. 8841cb0ef41Sopenharmony_ciEach item of the iterator is a JavaScript `Array`. The first item of the `Array` 8851cb0ef41Sopenharmony_ciis the `name`, the second item of the `Array` is the `value`. 8861cb0ef41Sopenharmony_ci 8871cb0ef41Sopenharmony_ciAlias for [`urlSearchParams[@@iterator]()`][`urlSearchParams@@iterator()`]. 8881cb0ef41Sopenharmony_ci 8891cb0ef41Sopenharmony_ci#### `urlSearchParams.forEach(fn[, thisArg])` 8901cb0ef41Sopenharmony_ci 8911cb0ef41Sopenharmony_ci<!-- YAML 8921cb0ef41Sopenharmony_cichanges: 8931cb0ef41Sopenharmony_ci - version: v18.0.0 8941cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 8951cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `fn` argument 8961cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 8971cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 8981cb0ef41Sopenharmony_ci--> 8991cb0ef41Sopenharmony_ci 9001cb0ef41Sopenharmony_ci* `fn` {Function} Invoked for each name-value pair in the query 9011cb0ef41Sopenharmony_ci* `thisArg` {Object} To be used as `this` value for when `fn` is called 9021cb0ef41Sopenharmony_ci 9031cb0ef41Sopenharmony_ciIterates over each name-value pair in the query and invokes the given function. 9041cb0ef41Sopenharmony_ci 9051cb0ef41Sopenharmony_ci```js 9061cb0ef41Sopenharmony_ciconst myURL = new URL('https://example.org/?a=b&c=d'); 9071cb0ef41Sopenharmony_cimyURL.searchParams.forEach((value, name, searchParams) => { 9081cb0ef41Sopenharmony_ci console.log(name, value, myURL.searchParams === searchParams); 9091cb0ef41Sopenharmony_ci}); 9101cb0ef41Sopenharmony_ci// Prints: 9111cb0ef41Sopenharmony_ci// a b true 9121cb0ef41Sopenharmony_ci// c d true 9131cb0ef41Sopenharmony_ci``` 9141cb0ef41Sopenharmony_ci 9151cb0ef41Sopenharmony_ci#### `urlSearchParams.get(name)` 9161cb0ef41Sopenharmony_ci 9171cb0ef41Sopenharmony_ci* `name` {string} 9181cb0ef41Sopenharmony_ci* Returns: {string} or `null` if there is no name-value pair with the given 9191cb0ef41Sopenharmony_ci `name`. 9201cb0ef41Sopenharmony_ci 9211cb0ef41Sopenharmony_ciReturns the value of the first name-value pair whose name is `name`. If there 9221cb0ef41Sopenharmony_ciare no such pairs, `null` is returned. 9231cb0ef41Sopenharmony_ci 9241cb0ef41Sopenharmony_ci#### `urlSearchParams.getAll(name)` 9251cb0ef41Sopenharmony_ci 9261cb0ef41Sopenharmony_ci* `name` {string} 9271cb0ef41Sopenharmony_ci* Returns: {string\[]} 9281cb0ef41Sopenharmony_ci 9291cb0ef41Sopenharmony_ciReturns the values of all name-value pairs whose name is `name`. If there are 9301cb0ef41Sopenharmony_cino such pairs, an empty array is returned. 9311cb0ef41Sopenharmony_ci 9321cb0ef41Sopenharmony_ci#### `urlSearchParams.has(name[, value])` 9331cb0ef41Sopenharmony_ci 9341cb0ef41Sopenharmony_ci<!-- YAML 9351cb0ef41Sopenharmony_cichanges: 9361cb0ef41Sopenharmony_ci - version: v18.18.0 9371cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/47885 9381cb0ef41Sopenharmony_ci description: Add support for optional `value` argument. 9391cb0ef41Sopenharmony_ci--> 9401cb0ef41Sopenharmony_ci 9411cb0ef41Sopenharmony_ci* `name` {string} 9421cb0ef41Sopenharmony_ci* `value` {string} 9431cb0ef41Sopenharmony_ci* Returns: {boolean} 9441cb0ef41Sopenharmony_ci 9451cb0ef41Sopenharmony_ciChecks if the `URLSearchParams` object contains key-value pair(s) based on 9461cb0ef41Sopenharmony_ci`name` and an optional `value` argument. 9471cb0ef41Sopenharmony_ci 9481cb0ef41Sopenharmony_ciIf `value` is provided, returns `true` when name-value pair with 9491cb0ef41Sopenharmony_cisame `name` and `value` exists. 9501cb0ef41Sopenharmony_ci 9511cb0ef41Sopenharmony_ciIf `value` is not provided, returns `true` if there is at least one name-value 9521cb0ef41Sopenharmony_cipair whose name is `name`. 9531cb0ef41Sopenharmony_ci 9541cb0ef41Sopenharmony_ci#### `urlSearchParams.keys()` 9551cb0ef41Sopenharmony_ci 9561cb0ef41Sopenharmony_ci* Returns: {Iterator} 9571cb0ef41Sopenharmony_ci 9581cb0ef41Sopenharmony_ciReturns an ES6 `Iterator` over the names of each name-value pair. 9591cb0ef41Sopenharmony_ci 9601cb0ef41Sopenharmony_ci```js 9611cb0ef41Sopenharmony_ciconst params = new URLSearchParams('foo=bar&foo=baz'); 9621cb0ef41Sopenharmony_cifor (const name of params.keys()) { 9631cb0ef41Sopenharmony_ci console.log(name); 9641cb0ef41Sopenharmony_ci} 9651cb0ef41Sopenharmony_ci// Prints: 9661cb0ef41Sopenharmony_ci// foo 9671cb0ef41Sopenharmony_ci// foo 9681cb0ef41Sopenharmony_ci``` 9691cb0ef41Sopenharmony_ci 9701cb0ef41Sopenharmony_ci#### `urlSearchParams.set(name, value)` 9711cb0ef41Sopenharmony_ci 9721cb0ef41Sopenharmony_ci* `name` {string} 9731cb0ef41Sopenharmony_ci* `value` {string} 9741cb0ef41Sopenharmony_ci 9751cb0ef41Sopenharmony_ciSets the value in the `URLSearchParams` object associated with `name` to 9761cb0ef41Sopenharmony_ci`value`. If there are any pre-existing name-value pairs whose names are `name`, 9771cb0ef41Sopenharmony_ciset the first such pair's value to `value` and remove all others. If not, 9781cb0ef41Sopenharmony_ciappend the name-value pair to the query string. 9791cb0ef41Sopenharmony_ci 9801cb0ef41Sopenharmony_ci```js 9811cb0ef41Sopenharmony_ciconst params = new URLSearchParams(); 9821cb0ef41Sopenharmony_ciparams.append('foo', 'bar'); 9831cb0ef41Sopenharmony_ciparams.append('foo', 'baz'); 9841cb0ef41Sopenharmony_ciparams.append('abc', 'def'); 9851cb0ef41Sopenharmony_ciconsole.log(params.toString()); 9861cb0ef41Sopenharmony_ci// Prints foo=bar&foo=baz&abc=def 9871cb0ef41Sopenharmony_ci 9881cb0ef41Sopenharmony_ciparams.set('foo', 'def'); 9891cb0ef41Sopenharmony_ciparams.set('xyz', 'opq'); 9901cb0ef41Sopenharmony_ciconsole.log(params.toString()); 9911cb0ef41Sopenharmony_ci// Prints foo=def&abc=def&xyz=opq 9921cb0ef41Sopenharmony_ci``` 9931cb0ef41Sopenharmony_ci 9941cb0ef41Sopenharmony_ci#### `urlSearchParams.size` 9951cb0ef41Sopenharmony_ci 9961cb0ef41Sopenharmony_ci<!-- YAML 9971cb0ef41Sopenharmony_ciadded: v18.16.0 9981cb0ef41Sopenharmony_ci--> 9991cb0ef41Sopenharmony_ci 10001cb0ef41Sopenharmony_ciThe total number of parameter entries. 10011cb0ef41Sopenharmony_ci 10021cb0ef41Sopenharmony_ci#### `urlSearchParams.sort()` 10031cb0ef41Sopenharmony_ci 10041cb0ef41Sopenharmony_ci<!-- YAML 10051cb0ef41Sopenharmony_ciadded: 10061cb0ef41Sopenharmony_ci - v7.7.0 10071cb0ef41Sopenharmony_ci - v6.13.0 10081cb0ef41Sopenharmony_ci--> 10091cb0ef41Sopenharmony_ci 10101cb0ef41Sopenharmony_ciSort all existing name-value pairs in-place by their names. Sorting is done 10111cb0ef41Sopenharmony_ciwith a [stable sorting algorithm][], so relative order between name-value pairs 10121cb0ef41Sopenharmony_ciwith the same name is preserved. 10131cb0ef41Sopenharmony_ci 10141cb0ef41Sopenharmony_ciThis method can be used, in particular, to increase cache hits. 10151cb0ef41Sopenharmony_ci 10161cb0ef41Sopenharmony_ci```js 10171cb0ef41Sopenharmony_ciconst params = new URLSearchParams('query[]=abc&type=search&query[]=123'); 10181cb0ef41Sopenharmony_ciparams.sort(); 10191cb0ef41Sopenharmony_ciconsole.log(params.toString()); 10201cb0ef41Sopenharmony_ci// Prints query%5B%5D=abc&query%5B%5D=123&type=search 10211cb0ef41Sopenharmony_ci``` 10221cb0ef41Sopenharmony_ci 10231cb0ef41Sopenharmony_ci#### `urlSearchParams.toString()` 10241cb0ef41Sopenharmony_ci 10251cb0ef41Sopenharmony_ci* Returns: {string} 10261cb0ef41Sopenharmony_ci 10271cb0ef41Sopenharmony_ciReturns the search parameters serialized as a string, with characters 10281cb0ef41Sopenharmony_cipercent-encoded where necessary. 10291cb0ef41Sopenharmony_ci 10301cb0ef41Sopenharmony_ci#### `urlSearchParams.values()` 10311cb0ef41Sopenharmony_ci 10321cb0ef41Sopenharmony_ci* Returns: {Iterator} 10331cb0ef41Sopenharmony_ci 10341cb0ef41Sopenharmony_ciReturns an ES6 `Iterator` over the values of each name-value pair. 10351cb0ef41Sopenharmony_ci 10361cb0ef41Sopenharmony_ci#### `urlSearchParams[Symbol.iterator]()` 10371cb0ef41Sopenharmony_ci 10381cb0ef41Sopenharmony_ci* Returns: {Iterator} 10391cb0ef41Sopenharmony_ci 10401cb0ef41Sopenharmony_ciReturns an ES6 `Iterator` over each of the name-value pairs in the query string. 10411cb0ef41Sopenharmony_ciEach item of the iterator is a JavaScript `Array`. The first item of the `Array` 10421cb0ef41Sopenharmony_ciis the `name`, the second item of the `Array` is the `value`. 10431cb0ef41Sopenharmony_ci 10441cb0ef41Sopenharmony_ciAlias for [`urlSearchParams.entries()`][]. 10451cb0ef41Sopenharmony_ci 10461cb0ef41Sopenharmony_ci```js 10471cb0ef41Sopenharmony_ciconst params = new URLSearchParams('foo=bar&xyz=baz'); 10481cb0ef41Sopenharmony_cifor (const [name, value] of params) { 10491cb0ef41Sopenharmony_ci console.log(name, value); 10501cb0ef41Sopenharmony_ci} 10511cb0ef41Sopenharmony_ci// Prints: 10521cb0ef41Sopenharmony_ci// foo bar 10531cb0ef41Sopenharmony_ci// xyz baz 10541cb0ef41Sopenharmony_ci``` 10551cb0ef41Sopenharmony_ci 10561cb0ef41Sopenharmony_ci### `url.domainToASCII(domain)` 10571cb0ef41Sopenharmony_ci 10581cb0ef41Sopenharmony_ci<!-- YAML 10591cb0ef41Sopenharmony_ciadded: 10601cb0ef41Sopenharmony_ci - v7.4.0 10611cb0ef41Sopenharmony_ci - v6.13.0 10621cb0ef41Sopenharmony_cichanges: 10631cb0ef41Sopenharmony_ci - version: v18.17.0 10641cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/47339 10651cb0ef41Sopenharmony_ci description: ICU requirement is removed. 10661cb0ef41Sopenharmony_ci--> 10671cb0ef41Sopenharmony_ci 10681cb0ef41Sopenharmony_ci* `domain` {string} 10691cb0ef41Sopenharmony_ci* Returns: {string} 10701cb0ef41Sopenharmony_ci 10711cb0ef41Sopenharmony_ciReturns the [Punycode][] ASCII serialization of the `domain`. If `domain` is an 10721cb0ef41Sopenharmony_ciinvalid domain, the empty string is returned. 10731cb0ef41Sopenharmony_ci 10741cb0ef41Sopenharmony_ciIt performs the inverse operation to [`url.domainToUnicode()`][]. 10751cb0ef41Sopenharmony_ci 10761cb0ef41Sopenharmony_ci```mjs 10771cb0ef41Sopenharmony_ciimport url from 'node:url'; 10781cb0ef41Sopenharmony_ci 10791cb0ef41Sopenharmony_ciconsole.log(url.domainToASCII('español.com')); 10801cb0ef41Sopenharmony_ci// Prints xn--espaol-zwa.com 10811cb0ef41Sopenharmony_ciconsole.log(url.domainToASCII('中文.com')); 10821cb0ef41Sopenharmony_ci// Prints xn--fiq228c.com 10831cb0ef41Sopenharmony_ciconsole.log(url.domainToASCII('xn--iñvalid.com')); 10841cb0ef41Sopenharmony_ci// Prints an empty string 10851cb0ef41Sopenharmony_ci``` 10861cb0ef41Sopenharmony_ci 10871cb0ef41Sopenharmony_ci```cjs 10881cb0ef41Sopenharmony_ciconst url = require('node:url'); 10891cb0ef41Sopenharmony_ci 10901cb0ef41Sopenharmony_ciconsole.log(url.domainToASCII('español.com')); 10911cb0ef41Sopenharmony_ci// Prints xn--espaol-zwa.com 10921cb0ef41Sopenharmony_ciconsole.log(url.domainToASCII('中文.com')); 10931cb0ef41Sopenharmony_ci// Prints xn--fiq228c.com 10941cb0ef41Sopenharmony_ciconsole.log(url.domainToASCII('xn--iñvalid.com')); 10951cb0ef41Sopenharmony_ci// Prints an empty string 10961cb0ef41Sopenharmony_ci``` 10971cb0ef41Sopenharmony_ci 10981cb0ef41Sopenharmony_ci### `url.domainToUnicode(domain)` 10991cb0ef41Sopenharmony_ci 11001cb0ef41Sopenharmony_ci<!-- YAML 11011cb0ef41Sopenharmony_ciadded: 11021cb0ef41Sopenharmony_ci - v7.4.0 11031cb0ef41Sopenharmony_ci - v6.13.0 11041cb0ef41Sopenharmony_cichanges: 11051cb0ef41Sopenharmony_ci - version: v18.17.0 11061cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/47339 11071cb0ef41Sopenharmony_ci description: ICU requirement is removed. 11081cb0ef41Sopenharmony_ci--> 11091cb0ef41Sopenharmony_ci 11101cb0ef41Sopenharmony_ci* `domain` {string} 11111cb0ef41Sopenharmony_ci* Returns: {string} 11121cb0ef41Sopenharmony_ci 11131cb0ef41Sopenharmony_ciReturns the Unicode serialization of the `domain`. If `domain` is an invalid 11141cb0ef41Sopenharmony_cidomain, the empty string is returned. 11151cb0ef41Sopenharmony_ci 11161cb0ef41Sopenharmony_ciIt performs the inverse operation to [`url.domainToASCII()`][]. 11171cb0ef41Sopenharmony_ci 11181cb0ef41Sopenharmony_ci```mjs 11191cb0ef41Sopenharmony_ciimport url from 'node:url'; 11201cb0ef41Sopenharmony_ci 11211cb0ef41Sopenharmony_ciconsole.log(url.domainToUnicode('xn--espaol-zwa.com')); 11221cb0ef41Sopenharmony_ci// Prints español.com 11231cb0ef41Sopenharmony_ciconsole.log(url.domainToUnicode('xn--fiq228c.com')); 11241cb0ef41Sopenharmony_ci// Prints 中文.com 11251cb0ef41Sopenharmony_ciconsole.log(url.domainToUnicode('xn--iñvalid.com')); 11261cb0ef41Sopenharmony_ci// Prints an empty string 11271cb0ef41Sopenharmony_ci``` 11281cb0ef41Sopenharmony_ci 11291cb0ef41Sopenharmony_ci```cjs 11301cb0ef41Sopenharmony_ciconst url = require('node:url'); 11311cb0ef41Sopenharmony_ci 11321cb0ef41Sopenharmony_ciconsole.log(url.domainToUnicode('xn--espaol-zwa.com')); 11331cb0ef41Sopenharmony_ci// Prints español.com 11341cb0ef41Sopenharmony_ciconsole.log(url.domainToUnicode('xn--fiq228c.com')); 11351cb0ef41Sopenharmony_ci// Prints 中文.com 11361cb0ef41Sopenharmony_ciconsole.log(url.domainToUnicode('xn--iñvalid.com')); 11371cb0ef41Sopenharmony_ci// Prints an empty string 11381cb0ef41Sopenharmony_ci``` 11391cb0ef41Sopenharmony_ci 11401cb0ef41Sopenharmony_ci### `url.fileURLToPath(url)` 11411cb0ef41Sopenharmony_ci 11421cb0ef41Sopenharmony_ci<!-- YAML 11431cb0ef41Sopenharmony_ciadded: v10.12.0 11441cb0ef41Sopenharmony_ci--> 11451cb0ef41Sopenharmony_ci 11461cb0ef41Sopenharmony_ci* `url` {URL | string} The file URL string or URL object to convert to a path. 11471cb0ef41Sopenharmony_ci* Returns: {string} The fully-resolved platform-specific Node.js file path. 11481cb0ef41Sopenharmony_ci 11491cb0ef41Sopenharmony_ciThis function ensures the correct decodings of percent-encoded characters as 11501cb0ef41Sopenharmony_ciwell as ensuring a cross-platform valid absolute path string. 11511cb0ef41Sopenharmony_ci 11521cb0ef41Sopenharmony_ci```mjs 11531cb0ef41Sopenharmony_ciimport { fileURLToPath } from 'node:url'; 11541cb0ef41Sopenharmony_ci 11551cb0ef41Sopenharmony_ciconst __filename = fileURLToPath(import.meta.url); 11561cb0ef41Sopenharmony_ci 11571cb0ef41Sopenharmony_cinew URL('file:///C:/path/').pathname; // Incorrect: /C:/path/ 11581cb0ef41Sopenharmony_cifileURLToPath('file:///C:/path/'); // Correct: C:\path\ (Windows) 11591cb0ef41Sopenharmony_ci 11601cb0ef41Sopenharmony_cinew URL('file://nas/foo.txt').pathname; // Incorrect: /foo.txt 11611cb0ef41Sopenharmony_cifileURLToPath('file://nas/foo.txt'); // Correct: \\nas\foo.txt (Windows) 11621cb0ef41Sopenharmony_ci 11631cb0ef41Sopenharmony_cinew URL('file:///你好.txt').pathname; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt 11641cb0ef41Sopenharmony_cifileURLToPath('file:///你好.txt'); // Correct: /你好.txt (POSIX) 11651cb0ef41Sopenharmony_ci 11661cb0ef41Sopenharmony_cinew URL('file:///hello world').pathname; // Incorrect: /hello%20world 11671cb0ef41Sopenharmony_cifileURLToPath('file:///hello world'); // Correct: /hello world (POSIX) 11681cb0ef41Sopenharmony_ci``` 11691cb0ef41Sopenharmony_ci 11701cb0ef41Sopenharmony_ci```cjs 11711cb0ef41Sopenharmony_ciconst { fileURLToPath } = require('node:url'); 11721cb0ef41Sopenharmony_cinew URL('file:///C:/path/').pathname; // Incorrect: /C:/path/ 11731cb0ef41Sopenharmony_cifileURLToPath('file:///C:/path/'); // Correct: C:\path\ (Windows) 11741cb0ef41Sopenharmony_ci 11751cb0ef41Sopenharmony_cinew URL('file://nas/foo.txt').pathname; // Incorrect: /foo.txt 11761cb0ef41Sopenharmony_cifileURLToPath('file://nas/foo.txt'); // Correct: \\nas\foo.txt (Windows) 11771cb0ef41Sopenharmony_ci 11781cb0ef41Sopenharmony_cinew URL('file:///你好.txt').pathname; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt 11791cb0ef41Sopenharmony_cifileURLToPath('file:///你好.txt'); // Correct: /你好.txt (POSIX) 11801cb0ef41Sopenharmony_ci 11811cb0ef41Sopenharmony_cinew URL('file:///hello world').pathname; // Incorrect: /hello%20world 11821cb0ef41Sopenharmony_cifileURLToPath('file:///hello world'); // Correct: /hello world (POSIX) 11831cb0ef41Sopenharmony_ci``` 11841cb0ef41Sopenharmony_ci 11851cb0ef41Sopenharmony_ci### `url.format(URL[, options])` 11861cb0ef41Sopenharmony_ci 11871cb0ef41Sopenharmony_ci<!-- YAML 11881cb0ef41Sopenharmony_ciadded: v7.6.0 11891cb0ef41Sopenharmony_ci--> 11901cb0ef41Sopenharmony_ci 11911cb0ef41Sopenharmony_ci* `URL` {URL} A [WHATWG URL][] object 11921cb0ef41Sopenharmony_ci* `options` {Object} 11931cb0ef41Sopenharmony_ci * `auth` {boolean} `true` if the serialized URL string should include the 11941cb0ef41Sopenharmony_ci username and password, `false` otherwise. **Default:** `true`. 11951cb0ef41Sopenharmony_ci * `fragment` {boolean} `true` if the serialized URL string should include the 11961cb0ef41Sopenharmony_ci fragment, `false` otherwise. **Default:** `true`. 11971cb0ef41Sopenharmony_ci * `search` {boolean} `true` if the serialized URL string should include the 11981cb0ef41Sopenharmony_ci search query, `false` otherwise. **Default:** `true`. 11991cb0ef41Sopenharmony_ci * `unicode` {boolean} `true` if Unicode characters appearing in the host 12001cb0ef41Sopenharmony_ci component of the URL string should be encoded directly as opposed to being 12011cb0ef41Sopenharmony_ci Punycode encoded. **Default:** `false`. 12021cb0ef41Sopenharmony_ci* Returns: {string} 12031cb0ef41Sopenharmony_ci 12041cb0ef41Sopenharmony_ciReturns a customizable serialization of a URL `String` representation of a 12051cb0ef41Sopenharmony_ci[WHATWG URL][] object. 12061cb0ef41Sopenharmony_ci 12071cb0ef41Sopenharmony_ciThe URL object has both a `toString()` method and `href` property that return 12081cb0ef41Sopenharmony_cistring serializations of the URL. These are not, however, customizable in 12091cb0ef41Sopenharmony_ciany way. The `url.format(URL[, options])` method allows for basic customization 12101cb0ef41Sopenharmony_ciof the output. 12111cb0ef41Sopenharmony_ci 12121cb0ef41Sopenharmony_ci```mjs 12131cb0ef41Sopenharmony_ciimport url from 'node:url'; 12141cb0ef41Sopenharmony_ciconst myURL = new URL('https://a:b@測試?abc#foo'); 12151cb0ef41Sopenharmony_ci 12161cb0ef41Sopenharmony_ciconsole.log(myURL.href); 12171cb0ef41Sopenharmony_ci// Prints https://a:b@xn--g6w251d/?abc#foo 12181cb0ef41Sopenharmony_ci 12191cb0ef41Sopenharmony_ciconsole.log(myURL.toString()); 12201cb0ef41Sopenharmony_ci// Prints https://a:b@xn--g6w251d/?abc#foo 12211cb0ef41Sopenharmony_ci 12221cb0ef41Sopenharmony_ciconsole.log(url.format(myURL, { fragment: false, unicode: true, auth: false })); 12231cb0ef41Sopenharmony_ci// Prints 'https://測試/?abc' 12241cb0ef41Sopenharmony_ci``` 12251cb0ef41Sopenharmony_ci 12261cb0ef41Sopenharmony_ci```cjs 12271cb0ef41Sopenharmony_ciconst url = require('node:url'); 12281cb0ef41Sopenharmony_ciconst myURL = new URL('https://a:b@測試?abc#foo'); 12291cb0ef41Sopenharmony_ci 12301cb0ef41Sopenharmony_ciconsole.log(myURL.href); 12311cb0ef41Sopenharmony_ci// Prints https://a:b@xn--g6w251d/?abc#foo 12321cb0ef41Sopenharmony_ci 12331cb0ef41Sopenharmony_ciconsole.log(myURL.toString()); 12341cb0ef41Sopenharmony_ci// Prints https://a:b@xn--g6w251d/?abc#foo 12351cb0ef41Sopenharmony_ci 12361cb0ef41Sopenharmony_ciconsole.log(url.format(myURL, { fragment: false, unicode: true, auth: false })); 12371cb0ef41Sopenharmony_ci// Prints 'https://測試/?abc' 12381cb0ef41Sopenharmony_ci``` 12391cb0ef41Sopenharmony_ci 12401cb0ef41Sopenharmony_ci### `url.pathToFileURL(path)` 12411cb0ef41Sopenharmony_ci 12421cb0ef41Sopenharmony_ci<!-- YAML 12431cb0ef41Sopenharmony_ciadded: v10.12.0 12441cb0ef41Sopenharmony_ci--> 12451cb0ef41Sopenharmony_ci 12461cb0ef41Sopenharmony_ci* `path` {string} The path to convert to a File URL. 12471cb0ef41Sopenharmony_ci* Returns: {URL} The file URL object. 12481cb0ef41Sopenharmony_ci 12491cb0ef41Sopenharmony_ciThis function ensures that `path` is resolved absolutely, and that the URL 12501cb0ef41Sopenharmony_cicontrol characters are correctly encoded when converting into a File URL. 12511cb0ef41Sopenharmony_ci 12521cb0ef41Sopenharmony_ci```mjs 12531cb0ef41Sopenharmony_ciimport { pathToFileURL } from 'node:url'; 12541cb0ef41Sopenharmony_ci 12551cb0ef41Sopenharmony_cinew URL('/foo#1', 'file:'); // Incorrect: file:///foo#1 12561cb0ef41Sopenharmony_cipathToFileURL('/foo#1'); // Correct: file:///foo%231 (POSIX) 12571cb0ef41Sopenharmony_ci 12581cb0ef41Sopenharmony_cinew URL('/some/path%.c', 'file:'); // Incorrect: file:///some/path%.c 12591cb0ef41Sopenharmony_cipathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX) 12601cb0ef41Sopenharmony_ci``` 12611cb0ef41Sopenharmony_ci 12621cb0ef41Sopenharmony_ci```cjs 12631cb0ef41Sopenharmony_ciconst { pathToFileURL } = require('node:url'); 12641cb0ef41Sopenharmony_cinew URL(__filename); // Incorrect: throws (POSIX) 12651cb0ef41Sopenharmony_cinew URL(__filename); // Incorrect: C:\... (Windows) 12661cb0ef41Sopenharmony_cipathToFileURL(__filename); // Correct: file:///... (POSIX) 12671cb0ef41Sopenharmony_cipathToFileURL(__filename); // Correct: file:///C:/... (Windows) 12681cb0ef41Sopenharmony_ci 12691cb0ef41Sopenharmony_cinew URL('/foo#1', 'file:'); // Incorrect: file:///foo#1 12701cb0ef41Sopenharmony_cipathToFileURL('/foo#1'); // Correct: file:///foo%231 (POSIX) 12711cb0ef41Sopenharmony_ci 12721cb0ef41Sopenharmony_cinew URL('/some/path%.c', 'file:'); // Incorrect: file:///some/path%.c 12731cb0ef41Sopenharmony_cipathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX) 12741cb0ef41Sopenharmony_ci``` 12751cb0ef41Sopenharmony_ci 12761cb0ef41Sopenharmony_ci### `url.urlToHttpOptions(url)` 12771cb0ef41Sopenharmony_ci 12781cb0ef41Sopenharmony_ci<!-- YAML 12791cb0ef41Sopenharmony_ciadded: 12801cb0ef41Sopenharmony_ci - v15.7.0 12811cb0ef41Sopenharmony_ci - v14.18.0 12821cb0ef41Sopenharmony_cichanges: 12831cb0ef41Sopenharmony_ci - version: v18.17.0 12841cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/46989 12851cb0ef41Sopenharmony_ci description: The returned object will also contain all the own enumerable 12861cb0ef41Sopenharmony_ci properties of the `url` argument. 12871cb0ef41Sopenharmony_ci--> 12881cb0ef41Sopenharmony_ci 12891cb0ef41Sopenharmony_ci* `url` {URL} The [WHATWG URL][] object to convert to an options object. 12901cb0ef41Sopenharmony_ci* Returns: {Object} Options object 12911cb0ef41Sopenharmony_ci * `protocol` {string} Protocol to use. 12921cb0ef41Sopenharmony_ci * `hostname` {string} A domain name or IP address of the server to issue the 12931cb0ef41Sopenharmony_ci request to. 12941cb0ef41Sopenharmony_ci * `hash` {string} The fragment portion of the URL. 12951cb0ef41Sopenharmony_ci * `search` {string} The serialized query portion of the URL. 12961cb0ef41Sopenharmony_ci * `pathname` {string} The path portion of the URL. 12971cb0ef41Sopenharmony_ci * `path` {string} Request path. Should include query string if any. 12981cb0ef41Sopenharmony_ci E.G. `'/index.html?page=12'`. An exception is thrown when the request path 12991cb0ef41Sopenharmony_ci contains illegal characters. Currently, only spaces are rejected but that 13001cb0ef41Sopenharmony_ci may change in the future. 13011cb0ef41Sopenharmony_ci * `href` {string} The serialized URL. 13021cb0ef41Sopenharmony_ci * `port` {number} Port of remote server. 13031cb0ef41Sopenharmony_ci * `auth` {string} Basic authentication i.e. `'user:password'` to compute an 13041cb0ef41Sopenharmony_ci Authorization header. 13051cb0ef41Sopenharmony_ci 13061cb0ef41Sopenharmony_ciThis utility function converts a URL object into an ordinary options object as 13071cb0ef41Sopenharmony_ciexpected by the [`http.request()`][] and [`https.request()`][] APIs. 13081cb0ef41Sopenharmony_ci 13091cb0ef41Sopenharmony_ci```mjs 13101cb0ef41Sopenharmony_ciimport { urlToHttpOptions } from 'node:url'; 13111cb0ef41Sopenharmony_ciconst myURL = new URL('https://a:b@測試?abc#foo'); 13121cb0ef41Sopenharmony_ci 13131cb0ef41Sopenharmony_ciconsole.log(urlToHttpOptions(myURL)); 13141cb0ef41Sopenharmony_ci/* 13151cb0ef41Sopenharmony_ci{ 13161cb0ef41Sopenharmony_ci protocol: 'https:', 13171cb0ef41Sopenharmony_ci hostname: 'xn--g6w251d', 13181cb0ef41Sopenharmony_ci hash: '#foo', 13191cb0ef41Sopenharmony_ci search: '?abc', 13201cb0ef41Sopenharmony_ci pathname: '/', 13211cb0ef41Sopenharmony_ci path: '/?abc', 13221cb0ef41Sopenharmony_ci href: 'https://a:b@xn--g6w251d/?abc#foo', 13231cb0ef41Sopenharmony_ci auth: 'a:b' 13241cb0ef41Sopenharmony_ci} 13251cb0ef41Sopenharmony_ci*/ 13261cb0ef41Sopenharmony_ci``` 13271cb0ef41Sopenharmony_ci 13281cb0ef41Sopenharmony_ci```cjs 13291cb0ef41Sopenharmony_ciconst { urlToHttpOptions } = require('node:url'); 13301cb0ef41Sopenharmony_ciconst myURL = new URL('https://a:b@測試?abc#foo'); 13311cb0ef41Sopenharmony_ci 13321cb0ef41Sopenharmony_ciconsole.log(urlToHttpOptions(myURL)); 13331cb0ef41Sopenharmony_ci/* 13341cb0ef41Sopenharmony_ci{ 13351cb0ef41Sopenharmony_ci protocol: 'https:', 13361cb0ef41Sopenharmony_ci hostname: 'xn--g6w251d', 13371cb0ef41Sopenharmony_ci hash: '#foo', 13381cb0ef41Sopenharmony_ci search: '?abc', 13391cb0ef41Sopenharmony_ci pathname: '/', 13401cb0ef41Sopenharmony_ci path: '/?abc', 13411cb0ef41Sopenharmony_ci href: 'https://a:b@xn--g6w251d/?abc#foo', 13421cb0ef41Sopenharmony_ci auth: 'a:b' 13431cb0ef41Sopenharmony_ci} 13441cb0ef41Sopenharmony_ci*/ 13451cb0ef41Sopenharmony_ci``` 13461cb0ef41Sopenharmony_ci 13471cb0ef41Sopenharmony_ci## Legacy URL API 13481cb0ef41Sopenharmony_ci 13491cb0ef41Sopenharmony_ci<!-- YAML 13501cb0ef41Sopenharmony_cichanges: 13511cb0ef41Sopenharmony_ci - version: 13521cb0ef41Sopenharmony_ci - v15.13.0 13531cb0ef41Sopenharmony_ci - v14.17.0 13541cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/37784 13551cb0ef41Sopenharmony_ci description: Deprecation revoked. Status changed to "Legacy". 13561cb0ef41Sopenharmony_ci - version: v11.0.0 13571cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/22715 13581cb0ef41Sopenharmony_ci description: This API is deprecated. 13591cb0ef41Sopenharmony_ci--> 13601cb0ef41Sopenharmony_ci 13611cb0ef41Sopenharmony_ci> Stability: 3 - Legacy: Use the WHATWG URL API instead. 13621cb0ef41Sopenharmony_ci 13631cb0ef41Sopenharmony_ci### Legacy `urlObject` 13641cb0ef41Sopenharmony_ci 13651cb0ef41Sopenharmony_ci<!-- YAML 13661cb0ef41Sopenharmony_cichanges: 13671cb0ef41Sopenharmony_ci - version: 13681cb0ef41Sopenharmony_ci - v15.13.0 13691cb0ef41Sopenharmony_ci - v14.17.0 13701cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/37784 13711cb0ef41Sopenharmony_ci description: Deprecation revoked. Status changed to "Legacy". 13721cb0ef41Sopenharmony_ci - version: v11.0.0 13731cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/22715 13741cb0ef41Sopenharmony_ci description: The Legacy URL API is deprecated. Use the WHATWG URL API. 13751cb0ef41Sopenharmony_ci--> 13761cb0ef41Sopenharmony_ci 13771cb0ef41Sopenharmony_ci> Stability: 3 - Legacy: Use the WHATWG URL API instead. 13781cb0ef41Sopenharmony_ci 13791cb0ef41Sopenharmony_ciThe legacy `urlObject` (`require('node:url').Url` or 13801cb0ef41Sopenharmony_ci`import { Url } from 'node:url'`) is 13811cb0ef41Sopenharmony_cicreated and returned by the `url.parse()` function. 13821cb0ef41Sopenharmony_ci 13831cb0ef41Sopenharmony_ci#### `urlObject.auth` 13841cb0ef41Sopenharmony_ci 13851cb0ef41Sopenharmony_ciThe `auth` property is the username and password portion of the URL, also 13861cb0ef41Sopenharmony_cireferred to as _userinfo_. This string subset follows the `protocol` and 13871cb0ef41Sopenharmony_cidouble slashes (if present) and precedes the `host` component, delimited by `@`. 13881cb0ef41Sopenharmony_ciThe string is either the username, or it is the username and password separated 13891cb0ef41Sopenharmony_ciby `:`. 13901cb0ef41Sopenharmony_ci 13911cb0ef41Sopenharmony_ciFor example: `'user:pass'`. 13921cb0ef41Sopenharmony_ci 13931cb0ef41Sopenharmony_ci#### `urlObject.hash` 13941cb0ef41Sopenharmony_ci 13951cb0ef41Sopenharmony_ciThe `hash` property is the fragment identifier portion of the URL including the 13961cb0ef41Sopenharmony_cileading `#` character. 13971cb0ef41Sopenharmony_ci 13981cb0ef41Sopenharmony_ciFor example: `'#hash'`. 13991cb0ef41Sopenharmony_ci 14001cb0ef41Sopenharmony_ci#### `urlObject.host` 14011cb0ef41Sopenharmony_ci 14021cb0ef41Sopenharmony_ciThe `host` property is the full lower-cased host portion of the URL, including 14031cb0ef41Sopenharmony_cithe `port` if specified. 14041cb0ef41Sopenharmony_ci 14051cb0ef41Sopenharmony_ciFor example: `'sub.example.com:8080'`. 14061cb0ef41Sopenharmony_ci 14071cb0ef41Sopenharmony_ci#### `urlObject.hostname` 14081cb0ef41Sopenharmony_ci 14091cb0ef41Sopenharmony_ciThe `hostname` property is the lower-cased host name portion of the `host` 14101cb0ef41Sopenharmony_cicomponent _without_ the `port` included. 14111cb0ef41Sopenharmony_ci 14121cb0ef41Sopenharmony_ciFor example: `'sub.example.com'`. 14131cb0ef41Sopenharmony_ci 14141cb0ef41Sopenharmony_ci#### `urlObject.href` 14151cb0ef41Sopenharmony_ci 14161cb0ef41Sopenharmony_ciThe `href` property is the full URL string that was parsed with both the 14171cb0ef41Sopenharmony_ci`protocol` and `host` components converted to lower-case. 14181cb0ef41Sopenharmony_ci 14191cb0ef41Sopenharmony_ciFor example: `'http://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'`. 14201cb0ef41Sopenharmony_ci 14211cb0ef41Sopenharmony_ci#### `urlObject.path` 14221cb0ef41Sopenharmony_ci 14231cb0ef41Sopenharmony_ciThe `path` property is a concatenation of the `pathname` and `search` 14241cb0ef41Sopenharmony_cicomponents. 14251cb0ef41Sopenharmony_ci 14261cb0ef41Sopenharmony_ciFor example: `'/p/a/t/h?query=string'`. 14271cb0ef41Sopenharmony_ci 14281cb0ef41Sopenharmony_ciNo decoding of the `path` is performed. 14291cb0ef41Sopenharmony_ci 14301cb0ef41Sopenharmony_ci#### `urlObject.pathname` 14311cb0ef41Sopenharmony_ci 14321cb0ef41Sopenharmony_ciThe `pathname` property consists of the entire path section of the URL. This 14331cb0ef41Sopenharmony_ciis everything following the `host` (including the `port`) and before the start 14341cb0ef41Sopenharmony_ciof the `query` or `hash` components, delimited by either the ASCII question 14351cb0ef41Sopenharmony_cimark (`?`) or hash (`#`) characters. 14361cb0ef41Sopenharmony_ci 14371cb0ef41Sopenharmony_ciFor example: `'/p/a/t/h'`. 14381cb0ef41Sopenharmony_ci 14391cb0ef41Sopenharmony_ciNo decoding of the path string is performed. 14401cb0ef41Sopenharmony_ci 14411cb0ef41Sopenharmony_ci#### `urlObject.port` 14421cb0ef41Sopenharmony_ci 14431cb0ef41Sopenharmony_ciThe `port` property is the numeric port portion of the `host` component. 14441cb0ef41Sopenharmony_ci 14451cb0ef41Sopenharmony_ciFor example: `'8080'`. 14461cb0ef41Sopenharmony_ci 14471cb0ef41Sopenharmony_ci#### `urlObject.protocol` 14481cb0ef41Sopenharmony_ci 14491cb0ef41Sopenharmony_ciThe `protocol` property identifies the URL's lower-cased protocol scheme. 14501cb0ef41Sopenharmony_ci 14511cb0ef41Sopenharmony_ciFor example: `'http:'`. 14521cb0ef41Sopenharmony_ci 14531cb0ef41Sopenharmony_ci#### `urlObject.query` 14541cb0ef41Sopenharmony_ci 14551cb0ef41Sopenharmony_ciThe `query` property is either the query string without the leading ASCII 14561cb0ef41Sopenharmony_ciquestion mark (`?`), or an object returned by the [`querystring`][] module's 14571cb0ef41Sopenharmony_ci`parse()` method. Whether the `query` property is a string or object is 14581cb0ef41Sopenharmony_cidetermined by the `parseQueryString` argument passed to `url.parse()`. 14591cb0ef41Sopenharmony_ci 14601cb0ef41Sopenharmony_ciFor example: `'query=string'` or `{'query': 'string'}`. 14611cb0ef41Sopenharmony_ci 14621cb0ef41Sopenharmony_ciIf returned as a string, no decoding of the query string is performed. If 14631cb0ef41Sopenharmony_cireturned as an object, both keys and values are decoded. 14641cb0ef41Sopenharmony_ci 14651cb0ef41Sopenharmony_ci#### `urlObject.search` 14661cb0ef41Sopenharmony_ci 14671cb0ef41Sopenharmony_ciThe `search` property consists of the entire "query string" portion of the 14681cb0ef41Sopenharmony_ciURL, including the leading ASCII question mark (`?`) character. 14691cb0ef41Sopenharmony_ci 14701cb0ef41Sopenharmony_ciFor example: `'?query=string'`. 14711cb0ef41Sopenharmony_ci 14721cb0ef41Sopenharmony_ciNo decoding of the query string is performed. 14731cb0ef41Sopenharmony_ci 14741cb0ef41Sopenharmony_ci#### `urlObject.slashes` 14751cb0ef41Sopenharmony_ci 14761cb0ef41Sopenharmony_ciThe `slashes` property is a `boolean` with a value of `true` if two ASCII 14771cb0ef41Sopenharmony_ciforward-slash characters (`/`) are required following the colon in the 14781cb0ef41Sopenharmony_ci`protocol`. 14791cb0ef41Sopenharmony_ci 14801cb0ef41Sopenharmony_ci### `url.format(urlObject)` 14811cb0ef41Sopenharmony_ci 14821cb0ef41Sopenharmony_ci<!-- YAML 14831cb0ef41Sopenharmony_ciadded: v0.1.25 14841cb0ef41Sopenharmony_cichanges: 14851cb0ef41Sopenharmony_ci - version: v17.0.0 14861cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/38631 14871cb0ef41Sopenharmony_ci description: Now throws an `ERR_INVALID_URL` exception when Punycode 14881cb0ef41Sopenharmony_ci conversion of a hostname introduces changes that could cause 14891cb0ef41Sopenharmony_ci the URL to be re-parsed differently. 14901cb0ef41Sopenharmony_ci - version: 14911cb0ef41Sopenharmony_ci - v15.13.0 14921cb0ef41Sopenharmony_ci - v14.17.0 14931cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/37784 14941cb0ef41Sopenharmony_ci description: Deprecation revoked. Status changed to "Legacy". 14951cb0ef41Sopenharmony_ci - version: v11.0.0 14961cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/22715 14971cb0ef41Sopenharmony_ci description: The Legacy URL API is deprecated. Use the WHATWG URL API. 14981cb0ef41Sopenharmony_ci - version: v7.0.0 14991cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/7234 15001cb0ef41Sopenharmony_ci description: URLs with a `file:` scheme will now always use the correct 15011cb0ef41Sopenharmony_ci number of slashes regardless of `slashes` option. A falsy 15021cb0ef41Sopenharmony_ci `slashes` option with no protocol is now also respected at all 15031cb0ef41Sopenharmony_ci times. 15041cb0ef41Sopenharmony_ci--> 15051cb0ef41Sopenharmony_ci 15061cb0ef41Sopenharmony_ci> Stability: 3 - Legacy: Use the WHATWG URL API instead. 15071cb0ef41Sopenharmony_ci 15081cb0ef41Sopenharmony_ci* `urlObject` {Object|string} A URL object (as returned by `url.parse()` or 15091cb0ef41Sopenharmony_ci constructed otherwise). If a string, it is converted to an object by passing 15101cb0ef41Sopenharmony_ci it to `url.parse()`. 15111cb0ef41Sopenharmony_ci 15121cb0ef41Sopenharmony_ciThe `url.format()` method returns a formatted URL string derived from 15131cb0ef41Sopenharmony_ci`urlObject`. 15141cb0ef41Sopenharmony_ci 15151cb0ef41Sopenharmony_ci```js 15161cb0ef41Sopenharmony_ciconst url = require('node:url'); 15171cb0ef41Sopenharmony_ciurl.format({ 15181cb0ef41Sopenharmony_ci protocol: 'https', 15191cb0ef41Sopenharmony_ci hostname: 'example.com', 15201cb0ef41Sopenharmony_ci pathname: '/some/path', 15211cb0ef41Sopenharmony_ci query: { 15221cb0ef41Sopenharmony_ci page: 1, 15231cb0ef41Sopenharmony_ci format: 'json', 15241cb0ef41Sopenharmony_ci }, 15251cb0ef41Sopenharmony_ci}); 15261cb0ef41Sopenharmony_ci 15271cb0ef41Sopenharmony_ci// => 'https://example.com/some/path?page=1&format=json' 15281cb0ef41Sopenharmony_ci``` 15291cb0ef41Sopenharmony_ci 15301cb0ef41Sopenharmony_ciIf `urlObject` is not an object or a string, `url.format()` will throw a 15311cb0ef41Sopenharmony_ci[`TypeError`][]. 15321cb0ef41Sopenharmony_ci 15331cb0ef41Sopenharmony_ciThe formatting process operates as follows: 15341cb0ef41Sopenharmony_ci 15351cb0ef41Sopenharmony_ci* A new empty string `result` is created. 15361cb0ef41Sopenharmony_ci* If `urlObject.protocol` is a string, it is appended as-is to `result`. 15371cb0ef41Sopenharmony_ci* Otherwise, if `urlObject.protocol` is not `undefined` and is not a string, an 15381cb0ef41Sopenharmony_ci [`Error`][] is thrown. 15391cb0ef41Sopenharmony_ci* For all string values of `urlObject.protocol` that _do not end_ with an ASCII 15401cb0ef41Sopenharmony_ci colon (`:`) character, the literal string `:` will be appended to `result`. 15411cb0ef41Sopenharmony_ci* If either of the following conditions is true, then the literal string `//` 15421cb0ef41Sopenharmony_ci will be appended to `result`: 15431cb0ef41Sopenharmony_ci * `urlObject.slashes` property is true; 15441cb0ef41Sopenharmony_ci * `urlObject.protocol` begins with `http`, `https`, `ftp`, `gopher`, or 15451cb0ef41Sopenharmony_ci `file`; 15461cb0ef41Sopenharmony_ci* If the value of the `urlObject.auth` property is truthy, and either 15471cb0ef41Sopenharmony_ci `urlObject.host` or `urlObject.hostname` are not `undefined`, the value of 15481cb0ef41Sopenharmony_ci `urlObject.auth` will be coerced into a string and appended to `result` 15491cb0ef41Sopenharmony_ci followed by the literal string `@`. 15501cb0ef41Sopenharmony_ci* If the `urlObject.host` property is `undefined` then: 15511cb0ef41Sopenharmony_ci * If the `urlObject.hostname` is a string, it is appended to `result`. 15521cb0ef41Sopenharmony_ci * Otherwise, if `urlObject.hostname` is not `undefined` and is not a string, 15531cb0ef41Sopenharmony_ci an [`Error`][] is thrown. 15541cb0ef41Sopenharmony_ci * If the `urlObject.port` property value is truthy, and `urlObject.hostname` 15551cb0ef41Sopenharmony_ci is not `undefined`: 15561cb0ef41Sopenharmony_ci * The literal string `:` is appended to `result`, and 15571cb0ef41Sopenharmony_ci * The value of `urlObject.port` is coerced to a string and appended to 15581cb0ef41Sopenharmony_ci `result`. 15591cb0ef41Sopenharmony_ci* Otherwise, if the `urlObject.host` property value is truthy, the value of 15601cb0ef41Sopenharmony_ci `urlObject.host` is coerced to a string and appended to `result`. 15611cb0ef41Sopenharmony_ci* If the `urlObject.pathname` property is a string that is not an empty string: 15621cb0ef41Sopenharmony_ci * If the `urlObject.pathname` _does not start_ with an ASCII forward slash 15631cb0ef41Sopenharmony_ci (`/`), then the literal string `'/'` is appended to `result`. 15641cb0ef41Sopenharmony_ci * The value of `urlObject.pathname` is appended to `result`. 15651cb0ef41Sopenharmony_ci* Otherwise, if `urlObject.pathname` is not `undefined` and is not a string, an 15661cb0ef41Sopenharmony_ci [`Error`][] is thrown. 15671cb0ef41Sopenharmony_ci* If the `urlObject.search` property is `undefined` and if the `urlObject.query` 15681cb0ef41Sopenharmony_ci property is an `Object`, the literal string `?` is appended to `result` 15691cb0ef41Sopenharmony_ci followed by the output of calling the [`querystring`][] module's `stringify()` 15701cb0ef41Sopenharmony_ci method passing the value of `urlObject.query`. 15711cb0ef41Sopenharmony_ci* Otherwise, if `urlObject.search` is a string: 15721cb0ef41Sopenharmony_ci * If the value of `urlObject.search` _does not start_ with the ASCII question 15731cb0ef41Sopenharmony_ci mark (`?`) character, the literal string `?` is appended to `result`. 15741cb0ef41Sopenharmony_ci * The value of `urlObject.search` is appended to `result`. 15751cb0ef41Sopenharmony_ci* Otherwise, if `urlObject.search` is not `undefined` and is not a string, an 15761cb0ef41Sopenharmony_ci [`Error`][] is thrown. 15771cb0ef41Sopenharmony_ci* If the `urlObject.hash` property is a string: 15781cb0ef41Sopenharmony_ci * If the value of `urlObject.hash` _does not start_ with the ASCII hash (`#`) 15791cb0ef41Sopenharmony_ci character, the literal string `#` is appended to `result`. 15801cb0ef41Sopenharmony_ci * The value of `urlObject.hash` is appended to `result`. 15811cb0ef41Sopenharmony_ci* Otherwise, if the `urlObject.hash` property is not `undefined` and is not a 15821cb0ef41Sopenharmony_ci string, an [`Error`][] is thrown. 15831cb0ef41Sopenharmony_ci* `result` is returned. 15841cb0ef41Sopenharmony_ci 15851cb0ef41Sopenharmony_ci### `url.parse(urlString[, parseQueryString[, slashesDenoteHost]])` 15861cb0ef41Sopenharmony_ci 15871cb0ef41Sopenharmony_ci<!-- YAML 15881cb0ef41Sopenharmony_ciadded: v0.1.25 15891cb0ef41Sopenharmony_cichanges: 15901cb0ef41Sopenharmony_ci - version: v18.13.0 15911cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/44919 15921cb0ef41Sopenharmony_ci description: Documentation-only deprecation. 15931cb0ef41Sopenharmony_ci - version: 15941cb0ef41Sopenharmony_ci - v15.13.0 15951cb0ef41Sopenharmony_ci - v14.17.0 15961cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/37784 15971cb0ef41Sopenharmony_ci description: Deprecation revoked. Status changed to "Legacy". 15981cb0ef41Sopenharmony_ci - version: v11.14.0 15991cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26941 16001cb0ef41Sopenharmony_ci description: The `pathname` property on the returned URL object is now `/` 16011cb0ef41Sopenharmony_ci when there is no path and the protocol scheme is `ws:` or 16021cb0ef41Sopenharmony_ci `wss:`. 16031cb0ef41Sopenharmony_ci - version: v11.0.0 16041cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/22715 16051cb0ef41Sopenharmony_ci description: The Legacy URL API is deprecated. Use the WHATWG URL API. 16061cb0ef41Sopenharmony_ci - version: v9.0.0 16071cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/13606 16081cb0ef41Sopenharmony_ci description: The `search` property on the returned URL object is now `null` 16091cb0ef41Sopenharmony_ci when no query string is present. 16101cb0ef41Sopenharmony_ci--> 16111cb0ef41Sopenharmony_ci 16121cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use the WHATWG URL API instead. 16131cb0ef41Sopenharmony_ci 16141cb0ef41Sopenharmony_ci* `urlString` {string} The URL string to parse. 16151cb0ef41Sopenharmony_ci* `parseQueryString` {boolean} If `true`, the `query` property will always 16161cb0ef41Sopenharmony_ci be set to an object returned by the [`querystring`][] module's `parse()` 16171cb0ef41Sopenharmony_ci method. If `false`, the `query` property on the returned URL object will be an 16181cb0ef41Sopenharmony_ci unparsed, undecoded string. **Default:** `false`. 16191cb0ef41Sopenharmony_ci* `slashesDenoteHost` {boolean} If `true`, the first token after the literal 16201cb0ef41Sopenharmony_ci string `//` and preceding the next `/` will be interpreted as the `host`. 16211cb0ef41Sopenharmony_ci For instance, given `//foo/bar`, the result would be 16221cb0ef41Sopenharmony_ci `{host: 'foo', pathname: '/bar'}` rather than `{pathname: '//foo/bar'}`. 16231cb0ef41Sopenharmony_ci **Default:** `false`. 16241cb0ef41Sopenharmony_ci 16251cb0ef41Sopenharmony_ciThe `url.parse()` method takes a URL string, parses it, and returns a URL 16261cb0ef41Sopenharmony_ciobject. 16271cb0ef41Sopenharmony_ci 16281cb0ef41Sopenharmony_ciA `TypeError` is thrown if `urlString` is not a string. 16291cb0ef41Sopenharmony_ci 16301cb0ef41Sopenharmony_ciA `URIError` is thrown if the `auth` property is present but cannot be decoded. 16311cb0ef41Sopenharmony_ci 16321cb0ef41Sopenharmony_ci`url.parse()` uses a lenient, non-standard algorithm for parsing URL 16331cb0ef41Sopenharmony_cistrings. It is prone to security issues such as [host name spoofing][] 16341cb0ef41Sopenharmony_ciand incorrect handling of usernames and passwords. Do not use with untrusted 16351cb0ef41Sopenharmony_ciinput. CVEs are not issued for `url.parse()` vulnerabilities. Use the 16361cb0ef41Sopenharmony_ci[WHATWG URL][] API instead. 16371cb0ef41Sopenharmony_ci 16381cb0ef41Sopenharmony_ci### `url.resolve(from, to)` 16391cb0ef41Sopenharmony_ci 16401cb0ef41Sopenharmony_ci<!-- YAML 16411cb0ef41Sopenharmony_ciadded: v0.1.25 16421cb0ef41Sopenharmony_cichanges: 16431cb0ef41Sopenharmony_ci - version: 16441cb0ef41Sopenharmony_ci - v15.13.0 16451cb0ef41Sopenharmony_ci - v14.17.0 16461cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/37784 16471cb0ef41Sopenharmony_ci description: Deprecation revoked. Status changed to "Legacy". 16481cb0ef41Sopenharmony_ci - version: v11.0.0 16491cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/22715 16501cb0ef41Sopenharmony_ci description: The Legacy URL API is deprecated. Use the WHATWG URL API. 16511cb0ef41Sopenharmony_ci - version: v6.6.0 16521cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/8215 16531cb0ef41Sopenharmony_ci description: The `auth` fields are now kept intact when `from` and `to` 16541cb0ef41Sopenharmony_ci refer to the same host. 16551cb0ef41Sopenharmony_ci - version: 16561cb0ef41Sopenharmony_ci - v6.5.0 16571cb0ef41Sopenharmony_ci - v4.6.2 16581cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/8214 16591cb0ef41Sopenharmony_ci description: The `port` field is copied correctly now. 16601cb0ef41Sopenharmony_ci - version: v6.0.0 16611cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/1480 16621cb0ef41Sopenharmony_ci description: The `auth` fields is cleared now the `to` parameter 16631cb0ef41Sopenharmony_ci contains a hostname. 16641cb0ef41Sopenharmony_ci--> 16651cb0ef41Sopenharmony_ci 16661cb0ef41Sopenharmony_ci> Stability: 3 - Legacy: Use the WHATWG URL API instead. 16671cb0ef41Sopenharmony_ci 16681cb0ef41Sopenharmony_ci* `from` {string} The base URL to use if `to` is a relative URL. 16691cb0ef41Sopenharmony_ci* `to` {string} The target URL to resolve. 16701cb0ef41Sopenharmony_ci 16711cb0ef41Sopenharmony_ciThe `url.resolve()` method resolves a target URL relative to a base URL in a 16721cb0ef41Sopenharmony_cimanner similar to that of a web browser resolving an anchor tag. 16731cb0ef41Sopenharmony_ci 16741cb0ef41Sopenharmony_ci```js 16751cb0ef41Sopenharmony_ciconst url = require('node:url'); 16761cb0ef41Sopenharmony_ciurl.resolve('/one/two/three', 'four'); // '/one/two/four' 16771cb0ef41Sopenharmony_ciurl.resolve('http://example.com/', '/one'); // 'http://example.com/one' 16781cb0ef41Sopenharmony_ciurl.resolve('http://example.com/one', '/two'); // 'http://example.com/two' 16791cb0ef41Sopenharmony_ci``` 16801cb0ef41Sopenharmony_ci 16811cb0ef41Sopenharmony_ciTo achieve the same result using the WHATWG URL API: 16821cb0ef41Sopenharmony_ci 16831cb0ef41Sopenharmony_ci```js 16841cb0ef41Sopenharmony_cifunction resolve(from, to) { 16851cb0ef41Sopenharmony_ci const resolvedUrl = new URL(to, new URL(from, 'resolve://')); 16861cb0ef41Sopenharmony_ci if (resolvedUrl.protocol === 'resolve:') { 16871cb0ef41Sopenharmony_ci // `from` is a relative URL. 16881cb0ef41Sopenharmony_ci const { pathname, search, hash } = resolvedUrl; 16891cb0ef41Sopenharmony_ci return pathname + search + hash; 16901cb0ef41Sopenharmony_ci } 16911cb0ef41Sopenharmony_ci return resolvedUrl.toString(); 16921cb0ef41Sopenharmony_ci} 16931cb0ef41Sopenharmony_ci 16941cb0ef41Sopenharmony_ciresolve('/one/two/three', 'four'); // '/one/two/four' 16951cb0ef41Sopenharmony_ciresolve('http://example.com/', '/one'); // 'http://example.com/one' 16961cb0ef41Sopenharmony_ciresolve('http://example.com/one', '/two'); // 'http://example.com/two' 16971cb0ef41Sopenharmony_ci``` 16981cb0ef41Sopenharmony_ci 16991cb0ef41Sopenharmony_ci<a id="whatwg-percent-encoding"></a> 17001cb0ef41Sopenharmony_ci 17011cb0ef41Sopenharmony_ci## Percent-encoding in URLs 17021cb0ef41Sopenharmony_ci 17031cb0ef41Sopenharmony_ciURLs are permitted to only contain a certain range of characters. Any character 17041cb0ef41Sopenharmony_cifalling outside of that range must be encoded. How such characters are encoded, 17051cb0ef41Sopenharmony_ciand which characters to encode depends entirely on where the character is 17061cb0ef41Sopenharmony_cilocated within the structure of the URL. 17071cb0ef41Sopenharmony_ci 17081cb0ef41Sopenharmony_ci### Legacy API 17091cb0ef41Sopenharmony_ci 17101cb0ef41Sopenharmony_ciWithin the Legacy API, spaces (`' '`) and the following characters will be 17111cb0ef41Sopenharmony_ciautomatically escaped in the properties of URL objects: 17121cb0ef41Sopenharmony_ci 17131cb0ef41Sopenharmony_ci```text 17141cb0ef41Sopenharmony_ci< > " ` \r \n \t { } | \ ^ ' 17151cb0ef41Sopenharmony_ci``` 17161cb0ef41Sopenharmony_ci 17171cb0ef41Sopenharmony_ciFor example, the ASCII space character (`' '`) is encoded as `%20`. The ASCII 17181cb0ef41Sopenharmony_ciforward slash (`/`) character is encoded as `%3C`. 17191cb0ef41Sopenharmony_ci 17201cb0ef41Sopenharmony_ci### WHATWG API 17211cb0ef41Sopenharmony_ci 17221cb0ef41Sopenharmony_ciThe [WHATWG URL Standard][] uses a more selective and fine grained approach to 17231cb0ef41Sopenharmony_ciselecting encoded characters than that used by the Legacy API. 17241cb0ef41Sopenharmony_ci 17251cb0ef41Sopenharmony_ciThe WHATWG algorithm defines four "percent-encode sets" that describe ranges 17261cb0ef41Sopenharmony_ciof characters that must be percent-encoded: 17271cb0ef41Sopenharmony_ci 17281cb0ef41Sopenharmony_ci* The _C0 control percent-encode set_ includes code points in range U+0000 to 17291cb0ef41Sopenharmony_ci U+001F (inclusive) and all code points greater than U+007E (\~). 17301cb0ef41Sopenharmony_ci 17311cb0ef41Sopenharmony_ci* The _fragment percent-encode set_ includes the _C0 control percent-encode set_ 17321cb0ef41Sopenharmony_ci and code points U+0020 SPACE, U+0022 ("), U+003C (<), U+003E (>), 17331cb0ef41Sopenharmony_ci and U+0060 (\`). 17341cb0ef41Sopenharmony_ci 17351cb0ef41Sopenharmony_ci* The _path percent-encode set_ includes the _C0 control percent-encode set_ 17361cb0ef41Sopenharmony_ci and code points U+0020 SPACE, U+0022 ("), U+0023 (#), U+003C (<), U+003E (>), 17371cb0ef41Sopenharmony_ci U+003F (?), U+0060 (\`), U+007B ({), and U+007D (}). 17381cb0ef41Sopenharmony_ci 17391cb0ef41Sopenharmony_ci* The _userinfo encode set_ includes the _path percent-encode set_ and code 17401cb0ef41Sopenharmony_ci points U+002F (/), U+003A (:), U+003B (;), U+003D (=), U+0040 (@), 17411cb0ef41Sopenharmony_ci U+005B (\[) to U+005E(^), and U+007C (|). 17421cb0ef41Sopenharmony_ci 17431cb0ef41Sopenharmony_ciThe _userinfo percent-encode set_ is used exclusively for username and 17441cb0ef41Sopenharmony_cipasswords encoded within the URL. The _path percent-encode set_ is used for the 17451cb0ef41Sopenharmony_cipath of most URLs. The _fragment percent-encode set_ is used for URL fragments. 17461cb0ef41Sopenharmony_ciThe _C0 control percent-encode set_ is used for host and path under certain 17471cb0ef41Sopenharmony_cispecific conditions, in addition to all other cases. 17481cb0ef41Sopenharmony_ci 17491cb0ef41Sopenharmony_ciWhen non-ASCII characters appear within a host name, the host name is encoded 17501cb0ef41Sopenharmony_ciusing the [Punycode][] algorithm. Note, however, that a host name _may_ contain 17511cb0ef41Sopenharmony_ci_both_ Punycode encoded and percent-encoded characters: 17521cb0ef41Sopenharmony_ci 17531cb0ef41Sopenharmony_ci```js 17541cb0ef41Sopenharmony_ciconst myURL = new URL('https://%CF%80.example.com/foo'); 17551cb0ef41Sopenharmony_ciconsole.log(myURL.href); 17561cb0ef41Sopenharmony_ci// Prints https://xn--1xa.example.com/foo 17571cb0ef41Sopenharmony_ciconsole.log(myURL.origin); 17581cb0ef41Sopenharmony_ci// Prints https://xn--1xa.example.com 17591cb0ef41Sopenharmony_ci``` 17601cb0ef41Sopenharmony_ci 17611cb0ef41Sopenharmony_ci[Punycode]: https://tools.ietf.org/html/rfc5891#section-4.4 17621cb0ef41Sopenharmony_ci[WHATWG URL]: #the-whatwg-url-api 17631cb0ef41Sopenharmony_ci[WHATWG URL Standard]: https://url.spec.whatwg.org/ 17641cb0ef41Sopenharmony_ci[`Error`]: errors.md#class-error 17651cb0ef41Sopenharmony_ci[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify 17661cb0ef41Sopenharmony_ci[`Map`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map 17671cb0ef41Sopenharmony_ci[`TypeError`]: errors.md#class-typeerror 17681cb0ef41Sopenharmony_ci[`URLSearchParams`]: #class-urlsearchparams 17691cb0ef41Sopenharmony_ci[`array.toString()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString 17701cb0ef41Sopenharmony_ci[`http.request()`]: http.md#httprequestoptions-callback 17711cb0ef41Sopenharmony_ci[`https.request()`]: https.md#httpsrequestoptions-callback 17721cb0ef41Sopenharmony_ci[`new URL()`]: #new-urlinput-base 17731cb0ef41Sopenharmony_ci[`querystring`]: querystring.md 17741cb0ef41Sopenharmony_ci[`url.domainToASCII()`]: #urldomaintoasciidomain 17751cb0ef41Sopenharmony_ci[`url.domainToUnicode()`]: #urldomaintounicodedomain 17761cb0ef41Sopenharmony_ci[`url.format()`]: #urlformaturlobject 17771cb0ef41Sopenharmony_ci[`url.href`]: #urlhref 17781cb0ef41Sopenharmony_ci[`url.parse()`]: #urlparseurlstring-parsequerystring-slashesdenotehost 17791cb0ef41Sopenharmony_ci[`url.search`]: #urlsearch 17801cb0ef41Sopenharmony_ci[`url.toJSON()`]: #urltojson 17811cb0ef41Sopenharmony_ci[`url.toString()`]: #urltostring 17821cb0ef41Sopenharmony_ci[`urlSearchParams.entries()`]: #urlsearchparamsentries 17831cb0ef41Sopenharmony_ci[`urlSearchParams@@iterator()`]: #urlsearchparamssymboliterator 17841cb0ef41Sopenharmony_ci[converted to a string]: https://tc39.es/ecma262/#sec-tostring 17851cb0ef41Sopenharmony_ci[examples of parsed URLs]: https://url.spec.whatwg.org/#example-url-parsing 17861cb0ef41Sopenharmony_ci[host name spoofing]: https://hackerone.com/reports/678487 17871cb0ef41Sopenharmony_ci[legacy `urlObject`]: #legacy-urlobject 17881cb0ef41Sopenharmony_ci[percent-encoded]: #percent-encoding-in-urls 17891cb0ef41Sopenharmony_ci[stable sorting algorithm]: https://en.wikipedia.org/wiki/Sorting_algorithm#Stability 1790