1# @ohos.uri (URI String Parsing)
2
3The uri module provides APIs for parsing URI strings that comply with the RFC3986 standard. This standard defines how to encode and parse the identifiers used to locate network resources. The module does not support parsing of URIs in non-standard scenarios.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9
10## Modules to Import
11
12```ts
13import { uri } from '@kit.ArkTS';
14```
15
16## URI
17
18Implements a URI, which provides APIs for determining whether objects are equal as well as standard paths.
19
20### Attributes
21
22**System capability**: SystemCapability.Utils.Lang
23
24| Name| Type| Readable| Writable| Description|
25| -------- | -------- | -------- | -------- | -------- |
26| scheme | string | Yes| No| Scheme in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
27| userInfo | string | Yes| No| User information in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
28| host | string | Yes| No| Host name (without the port number) in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
29| port | string | Yes| No| Port number in the URI.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
30| path | string | Yes| No| Path in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
31| query | string | Yes| No| Query parameters in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
32| fragment | string | Yes| No| Fragments in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
33| authority | string | Yes| No| Authority in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
34| ssp | string | Yes| No| Scheme-specific part in the URI. It contains protocol-or scheme-specific information.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
35| encodedUserInfo<sup>12+</sup>  | string | Yes  | No  | Encoded user information in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 12.  |
36| encodedPath<sup>12+</sup>      | string | Yes  | No  | Encoded path in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 12.      |
37| encodedQuery<sup>12+</sup>     | string | Yes  | No  | Encoded query parameters in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 12.     |
38| encodedFragment<sup>12+</sup>  | string | Yes  | No  | Encoded fragments in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 12.     |
39| encodedAuthority<sup>12+</sup> | string | Yes  | No  | Encoded authority in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 12.  |
40| encodedSSP<sup>12+</sup>       | string | Yes  | No  | Encoded scheme-specific part in the URI.<br>**Atomic service API**: This API can be used in atomic services since API version 12.  |
41
42### Naming Rules
43
44Naming format:
45
46A standard URI mainly consists of three parts, as follows:
47
48[scheme:]scheme-specific-part[#fragment]
49
50The generic URI syntax consists of a hierarchical sequence of components, as follows:
51
52[scheme:][//authority][path][?query][#fragment]
53
54It can be further divided into the following parts:
55
56[scheme:][//[user-info@]host[:port]][path][?query][#fragment]
57
58- scheme: scheme name, which is separated from scheme-specific-part by a colon (:). The URI that contains the scheme component is an absolute URI, and the URI that does not contain the scheme component is a relative URI. Set this part as required. Example values: **http**, **https**, **ftp**, and **datashare**.
59- scheme-specific-part: specific part of the URI decoding scheme. It is located between [scheme:] and [#fragment] and consists of [//][authority][path][?query]. The URI that starts with a slash (/) is a hierarchical URI, and the URI that does not start with a slash (/) is an opaque URI. Set this part as required.
60    - authority: decoding authority component of the URI. The value consists of [userinfo@]host[:port]. Set this part as required.
61        - userinfo: user information, which is separated from host by an at sign (@). Set this part as required.
62        - host: host name of the server. This parameter is mandatory when authority exists.
63        - port: port number of the server. The default value is **-1**. Set this part as required.
64    - path: path information, which is located between host and query and separated by a slash (/). Set this part as required.
65    - query: query component, which is located between path and fragment, indicated by the first question mark (?) character, and is in the format of key-value pairs. Multiple key-value pairs are separated by the at sign (&), and the key and value in a pair is separated by the equal sign (=). Set this part as required.
66- fragment: fragment component, which is separated from scheme-specific-part by the pound key (#). Set this part as required.
67
68**Example URIs**
69
70```ts
71const uriObj1 = new uri.URI("ftp://ftp.aaa.bbb.ccc/dddd/eee.txt");
72console.info(uriObj1.host) // ftp.aaa.bbb.ccc
73console.info(uriObj1.fragment) // null
74console.info(uriObj1.path) // /dddd/eee.txt
75console.info(uriObj1.scheme) // ftp
76console.info(uriObj1.userInfo) // null
77console.info(uriObj1.port) // -1
78console.info(uriObj1.query) // null
79
80const uriObj2 = new uri.URI("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#fragment");
81console.info(uriObj2.host) // spinaltap.micro.umn.edu
82console.info(uriObj2.fragment) // fragment
83console.info(uriObj2.path) // /00/Weather/California/Los Angeles
84console.info(uriObj2.scheme) // gopher
85console.info(uriObj2.userInfo) // null
86console.info(uriObj2.port) //-1
87console.info(uriObj2.query) // null
88
89const uriObj3 = new uri.URI("datashare:///com.samples.datasharetest.DataShare/DB00/TBL00");
90console.info(uriObj3.host) // null
91console.info(uriObj3.fragment) // null
92console.info(uriObj3.path) // /com.samples.datasharetest.DataShare/DB00/TBL00
93console.info(uriObj3.scheme) // datashare
94console.info(uriObj3.userInfo) // null
95console.info(uriObj3.port) // -1
96console.info(uriObj3.query) // null
97
98const uriObj4 = new uri.URI("https://username:password@host:8080/directory/file?foo=1&bar=2#fragment");
99console.info(uriObj4.host) // host
100console.info(uriObj4.fragment) // fragment
101console.info(uriObj4.path) // /directory/file
102console.info(uriObj4.scheme) // https
103console.info(uriObj4.userInfo) // username:password
104console.info(uriObj4.port) // 8080
105console.info(uriObj4.query) // foo=1&bar=2
106
107const uriObj5 = new uri.URI("dataability:///com.example.DataAbility");
108console.info(uriObj5.host) // null
109console.info(uriObj5.fragment) // null
110console.info(uriObj5.path) // /com.example.DataAbility:
111console.info(uriObj5.scheme) // dataability
112console.info(uriObj5.userInfo) // null
113console.info(uriObj5.port) // -1
114console.info(uriObj5.query) // null
115
116const uriObj6 = new uri.URI("https://username:my+name@host:8080/directory/my+file?foo=1&bar=2#fragment");
117console.info(uriObj6.encodedUserInfo) // username:my+name
118console.info(uriObj6.encodedPath) // /directory/my+file
119console.info(uriObj6.encodedQuery) // foo=1&bar=2
120console.info(uriObj6.encodedFragment) // fragment
121console.info(uriObj6.encodedAuthority) // username:my+name@host:8080
122console.info(uriObj6.encodedSSP) // //username:my+name@host:8080/directory/my+file?foo=1&bar=2
123
124let uriObj7 = new uri.URI("www.abc.com:8080/directory/file?ab=pppppp#qwer=da");
125console.log(uriObj7.scheme) // www.abc.com
126console.log(uriObj7.host) // null
127console.log(uriObj7.port) // -1
128console.log(uriObj7.path) // null
129console.log(uriObj7.query) // null
130console.log(uriObj7.authority) // null
131console.log(uriObj7.fragment) // qwer=da
132console.log(uriObj7.ssp) // 8080/directory/file?ab=pppppp
133console.log("result:", uriObj7.checkIsAbsolute()) // result: true
134```
135
136### constructor
137
138constructor(uri: string)
139
140A constructor used to create a URI instance.
141
142**Atomic service API**: This API can be used in atomic services since API version 11.
143
144**System capability**: SystemCapability.Utils.Lang
145
146**Parameters**
147
148| Name| Type| Mandatory| Description|
149| -------- | -------- | -------- | -------- |
150| uri | string | Yes| Input object.|
151
152**Error codes**
153
154For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
155
156| ID| Error Message|
157| -------- | -------- |
158| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
159| 10200002 | Invalid uri string. |
160
161**Example**
162
163```ts
164let mm = 'https://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
165new uri.URI(mm);
166```
167```ts
168new uri.URI('https://username:password@host:8080');
169```
170
171
172### toString
173
174toString(): string
175
176Converts this URI into an encoded string.
177
178**System capability**: SystemCapability.Utils.Lang
179
180**Atomic service API**: This API can be used in atomic services since API version 11.
181
182**Return value**
183
184| Type| Description|
185| -------- | -------- |
186| string | URI in a serialized string.|
187
188**Example**
189
190```ts
191const result = new uri.URI('https://username:password@host:8080/directory/file?ab=pppppp#qwer da');
192let result1 = result.toString(); // https://username:password@host:8080/directory/file?ab=pppppp#qwer%20da
193```
194
195### equalsTo<sup>9+</sup>
196
197equalsTo(other: URI): boolean
198
199Checks whether this URI is the same as another URI object.
200
201**Atomic service API**: This API can be used in atomic services since API version 11.
202
203**System capability**: SystemCapability.Utils.Lang
204
205**Parameters**
206
207| Name| Type| Mandatory| Description|
208| -------- | -------- | -------- | -------- |
209| other | [URI](#uri) | Yes| URI object to compare.|
210
211**Return value**
212
213| Type| Description|
214| -------- | -------- |
215| boolean | Returns **true** if the two URIs are the same; returns **false** otherwise.|
216
217**Error codes**
218
219For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
220
221| ID| Error Message|
222| -------- | -------- |
223| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
224
225**Example**
226
227```ts
228const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
229const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
230let result = uriInstance.equalsTo(uriInstance1); // true
231```
232
233### checkIsAbsolute
234
235checkIsAbsolute(): boolean
236
237Checks whether this URI is an absolute URI (whether the scheme component is defined).
238
239**Atomic service API**: This API can be used in atomic services since API version 11.
240
241**System capability**: SystemCapability.Utils.Lang
242
243**Return value**
244
245| Type| Description|
246| -------- | -------- |
247| boolean | **true**: The URI is an absolute URI.<br>**false**: The URI is not an absolute URI.|
248
249**Example**
250
251```ts
252const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080?query=pppppp');
253console.info(`${uriInstance.checkIsAbsolute()}`); // true
254const uriInstance1 = new uri.URI('xxx.com/suppliers.htm');
255console.info(`${uriInstance1.checkIsAbsolute()}`); // false
256```
257
258
259### normalize
260
261normalize(): URI
262
263Normalizes the path of this URI.
264
265> **NOTE**
266>
267> If the URI is opaque or its path is already in normalized, the URI is directly returned. Otherwise, a new URI is created. The new URI is similar to the current URI. The only difference relies on its path, which is determined by normalizing the path of the current URI according to the following guidelines:
268>
269> - All . (dot) segments are removed.
270> - For any .. (double-dot) segment that is immediately preceded by a segment that is not .., both segments are removed. This process is iterated until no further removals can be made.
271>
272> If normalization results in a path starting with a .. (double-dot) segment, it indicates that there were insufficient preceding non-.. segments for removal. As a result, the path will start with a .. segment.
273
274
275**Atomic service API**: This API can be used in atomic services since API version 11.
276
277**System capability**: SystemCapability.Utils.Lang
278
279**Return value**
280
281| Type| Description|
282| -------- | -------- |
283| [URI](#uri) | URI with the normalized path.|
284
285**Example**
286
287```ts
288const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080/path/path1/../path2/./path3?query=pppppp');
289console.info(uriInstance.path); // /path/path1/../path2/./path3
290// Following path normalization, all . (dot) segments are removed. If a .. (double-dot) segment is immediately preceded by a segment that is not .., both segments are removed.
291let uriInstance1 = uriInstance.normalize();
292console.info(uriInstance1.path); // /path/path2/path3
293let uri1 = new uri.URI('http://www.test.com/../../patch/path1/../path2/path3/./path4/../');
294console.log(uri1.path); // /../../patch/path1/../path2/path3/./path4/../
295// If normalization result in a path starting with a .. (double-dot) segment, it indicates that there were insufficient preceding non-.. segments for removal. As a result, the path will start with a .. segment.
296let uri2 = uri1.normalize();
297console.log(uri2.path); // /../../patch/path2/path3
298```
299
300### checkRelative<sup>12+</sup>
301
302checkRelative(): boolean
303
304Checks whether this URI is a relative URI. A relative URI does not contain the scheme component.
305
306**Atomic service API**: This API can be used in atomic services since API version 12.
307
308**System capability**: SystemCapability.Utils.Lang
309
310**Return value**
311
312| Type   | Description                                      |
313| ------- | ------------------------------------------ |
314| boolean | **true**: The URI is a relative URI.<br>**false**: The URI is not a relative URI.|
315
316**Example**
317
318```ts
319const uriInstance = new uri.URI("https://username:password@www.qwer.com:8080?query=p");
320console.info(`${uriInstance.checkRelative()}`); // false
321const uriInstance1 = new uri.URI("/images/pic.jpg");
322console.info(`${uriInstance1.checkRelative()}`); // true
323```
324
325### checkOpaque<sup>12+</sup>
326
327checkOpaque(): boolean
328
329Checks whether this URI is an opaque URI. The URI that does not start with a slash (/) is an opaque URI.
330
331**Atomic service API**: This API can be used in atomic services since API version 12.
332
333**System capability**: SystemCapability.Utils.Lang
334
335**Return value**
336
337| Type   | Description                                          |
338| ------- | ---------------------------------------------- |
339| boolean | **true**: The URI is an opaque URI.<br>**false**: The URI is not an opaque URI.|
340
341**Example**
342
343```ts
344const uriInstance = new uri.URI("http://www.test.com/images/pic.jpg");
345console.info(`${uriInstance.checkOpaque()}`); // false
346const uriInstance1 = new uri.URI("mailto:user@example.com");
347console.info(`${uriInstance1.checkOpaque()}`); // true
348```
349
350### checkHierarchical<sup>12+</sup>
351
352checkHierarchical(): boolean
353
354Checks whether this URI is a hierarchical URI. The URI that starts with a slash (/) in scheme-specific-part is a hierarchical URI. Relative URIs are also hierarchical.
355
356**Atomic service API**: This API can be used in atomic services since API version 12.
357
358**System capability**: SystemCapability.Utils.Lang
359
360**Return value**
361
362| Type   | Description                                        |
363| ------- | -------------------------------------------- |
364| boolean | **true**: The URI is a hierarchical URI.<br>**false**: The URI is not a hierarchical URI.|
365
366**Example**
367
368```ts
369const uriInstance = new uri.URI("http://www.test.com/images/pic.jpg");
370console.info(`${uriInstance.checkHierarchical()}`); // true
371const uriInstance1 = new uri.URI("mailto:user@example.com");
372console.info(`${uriInstance1.checkHierarchical()}`); // false
373```
374
375### getQueryValue<sup>12+</sup>
376
377getQueryValue(key:string): string
378
379Obtains the first value of a given key from the query component of this URI. If the query component contains encoded content, this API decodes the key before obtaining the value.
380
381The query component follows the question mark (?) and consists of key-value pairs, separated by the at sign (&). In each key-value pair, the equal sign (=) is used to connect the key and value.
382
383**Atomic service API**: This API can be used in atomic services since API version 12.
384
385**System capability**: SystemCapability.Utils.Lang
386
387**Parameters**
388
389| Name| Type  | Mandatory| Description                   |
390| ------ | ------ | ---- | ----------------------- |
391| key    | string | Yes  | Key of the URI query parameter.|
392
393**Return value**
394
395| Type  | Description                         |
396| ------ | ----------------------------- |
397| string | First value obtained. If no value is found, a null object is returned.|
398
399**Error codes**
400
401For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
402
403| ID| Error Message|
404| -------- | -------- |
405| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
406
407**Example**
408
409```ts
410const uriInstance = new uri.URI("https://www.com?param1=value1&param2=value2");
411console.info(uriInstance.getQueryValue("param1")); // value1
412let uriInstance1 = new uri.URI('https://www.zyy.ss?sa%3D=po%7E');
413console.info(uriInstance1.getQueryValue('sa=')) // po~
414console.info(uriInstance1.getQueryValue('abc')) // null
415```
416
417### addQueryValue<sup>12+</sup>
418
419addQueryValue(key:string, value:string): URI
420
421Adds a query parameter to this URI to create a new URI, while keeping the existing URI unchanged.
422
423**Atomic service API**: This API can be used in atomic services since API version 12.
424
425**System capability**: SystemCapability.Utils.Lang
426
427**Parameters**
428
429| Name| Type  | Mandatory| Description                    |
430| ------ | ------ | ---- | ------------------------ |
431| key    | string | Yes  | Key of the query parameter.|
432| value  | string | Yes  | Value of the query parameter.  |
433
434**Return value**
435
436| Type| Description                            |
437| ---- | -------------------------------- |
438| [URI](#uri)  | URI object with the query parameter.|
439
440**Error codes**
441
442For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
443
444| ID| Error Message|
445| -------- | -------- |
446| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
447
448**Example**
449
450```ts
451const uriInstance = new uri.URI("https://www.test.com");
452const newRoute = uriInstance.addQueryValue("param1", "hello world");
453console.info(newRoute.toString()); // https://www.test.com?param1=hello%20world
454```
455
456### addSegment<sup>12+</sup>
457
458addSegment(pathSegment:string): URI
459
460Encodes a given field, appends it to the path component of this URI to create a new URI, and returns the new URI, while keeping the existing URI unchanged.
461
462**Atomic service API**: This API can be used in atomic services since API version 12.
463
464**System capability**: SystemCapability.Utils.Lang
465
466**Parameters**
467
468| Name     | Type  | Mandatory| Description              |
469| ----------- | ------ | ---- | ------------------ |
470| pathSegment | string | Yes  | Field to be appended to the path component.|
471
472**Return value**
473
474| Type| Description                            |
475| ---- | -------------------------------- |
476| [URI](#uri)  | URI object with the appended field.|
477
478**Error codes**
479
480For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
481
482| ID| Error Message|
483| -------- | -------- |
484| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
485
486**Example**
487
488```ts
489const uriInstance = new uri.URI("http://www.test.com");
490const newRoute = uriInstance.addSegment("my image.jpg");
491console.info(newRoute.toString()); // http://www.test.com/my%20image.jpg
492```
493
494### addEncodedSegment<sup>12+</sup>
495
496addEncodedSegment(pathSegment:string): URI
497
498Appends an encoded field to the path component of this URI to create a new URI and returns the new URI, while keeping the existing URI unchanged.
499
500**Atomic service API**: This API can be used in atomic services since API version 12.
501
502**System capability**: SystemCapability.Utils.Lang
503
504**Parameters**
505
506| Name     | Type  | Mandatory| Description              |
507| ----------- | ------ | ---- | ------------------ |
508| pathSegment | string | Yes  | Encoded field to be appended to the path component.|
509
510**Return value**
511
512| Type| Description                            |
513| ---- | -------------------------------- |
514| [URI](#uri)  | URI object with the appended field.|
515
516**Error codes**
517
518For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
519
520| ID| Error Message|
521| -------- | -------- |
522| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
523
524**Example**
525
526```ts
527const uriInstance = new uri.URI("http://www.test.com");
528const newRoute = uriInstance.addEncodedSegment("my%20image.jpg");
529console.info(newRoute.toString()); // http://www.test.com/my%20image.jpg
530```
531
532### getQueryNames<sup>12+</sup>
533
534getQueryNames(): string[]
535
536Obtains all non-repeated keys in the query component of this URI. The query component follows the question mark (?) and consists of key-value pairs, separated by the at sign (&). In each key-value pair, the equal sign (=) is used to connect the key and value.
537
538**Atomic service API**: This API can be used in atomic services since API version 12.
539
540**System capability**: SystemCapability.Utils.Lang
541
542**Return value**
543
544| Type       | Description                               |
545| ----------- | ----------------------------------- |
546| string[] | Non-repeated keys in the query component.|
547
548**Example**
549
550```ts
551const uriInstance = new uri.URI("https://www.test.com?param1=value1&param2=value2");
552const paramNames = uriInstance.getQueryNames();
553console.info(Array.from(paramNames).toString()); // param1,param2
554```
555
556### getQueryValues<sup>12+</sup>
557
558getQueryValues(key:string): string[]
559
560Obtains the values of a given key from the query component of this URI. If the query component contains encoded content, this API decodes the keys before obtaining the values.
561
562The query component follows the question mark (?) and consists of key-value pairs, separated by the at sign (&). In each key-value pair, the equal sign (=) is used to connect the key and value.
563
564**Atomic service API**: This API can be used in atomic services since API version 12.
565
566**System capability**: SystemCapability.Utils.Lang
567
568**Parameters**
569
570| Name| Type  | Mandatory| Description                   |
571| ------ | ------ | ---- | ----------------------- |
572| key    | string | Yes  | Key of the URI query parameter.|
573
574**Return value**
575
576| Type    | Description                               |
577| -------- | ----------------------------------- |
578| string[] | Array of values obtained. If no value is found, an empty string array [] is returned.|
579
580**Error codes**
581
582For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
583
584| ID| Error Message|
585| -------- | -------- |
586| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
587
588**Example**
589
590```ts
591const uriInstance = new uri.URI("https://www.test.com/search?query=name&query=my");
592console.info(uriInstance.getQueryValues("query").toString()); // name,my
593console.info(JSON.stringify(uriInstance.getQueryValues("abc"))); // []
594```
595
596### getBooleanQueryValue<sup>12+</sup>
597
598getBooleanQueryValue(key:string,defaultValue:boolean): boolean
599
600Obtains the value of the Boolean type of a query parameter in this URI.
601
602**Atomic service API**: This API can be used in atomic services since API version 12.
603
604**System capability**: SystemCapability.Utils.Lang
605
606**Parameters**
607
608| Name      | Type   | Mandatory| Description                                 |
609| ------------ | ------- | ---- | ------------------------------------- |
610| key          | string  | Yes  | Name of the query parameter.              |
611| defaultValue | boolean | Yes  | Default value.|
612
613**Return value**
614
615| Type   | Description                                                                  |
616| ------- | ---------------------------------------------------------------------- |
617| boolean | If the specified query parameter does not exist, the default value is returned. If the first value of the query parameter is **false** or **0**, **false** is returned. Otherwise, **true** is returned.|
618
619**Error codes**
620
621For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
622
623| ID| Error Message|
624| -------- | -------- |
625| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
626
627**Example**
628
629```ts
630const uriInstance = new uri.URI("https://www.test.com/search?active=true");
631console.info(`${uriInstance.getBooleanQueryValue("active", false)}`); // true
632const uriInstance1 = new uri.URI("https://www.test.com/search");
633console.info(`${uriInstance1.getBooleanQueryValue("active", false)}`); // false
634const uriInstance2 = new uri.URI("https://www.test.com/search?active=aa&active=false");
635console.info(`${uriInstance2.getBooleanQueryValue("active", false)}`); // true
636const uriInstance3 = new uri.URI("https://www.test.com/search?active=0");
637console.info(`${uriInstance3.getBooleanQueryValue("active", true)}`); // false
638```
639
640### clearQuery<sup>12+</sup>
641
642clearQuery(): URI
643
644Clears the query component of this URI to create a new URI, while keeping the existing URI object unchanged.
645
646**Atomic service API**: This API can be used in atomic services since API version 12.
647
648**System capability**: SystemCapability.Utils.Lang
649
650**Return value**
651
652| Type| Description                                 |
653| ---- | ------------------------------------- |
654| [URI](#uri)  | URI object whose query component has been cleared.|
655
656**Example**
657
658```ts
659const uriInstance = new uri.URI("https://www.test.com?param1=value1");
660console.info(uriInstance.clearQuery().toString()); // https://www.test.com
661```
662
663### getLastSegment<sup>12+</sup>
664
665getLastSegment(): string
666
667Obtains the last segment of this URI. A path includes multiple segments, separated by slashes (/). The part that ends with a slash is not a segment.
668
669**Atomic service API**: This API can be used in atomic services since API version 12.
670
671**System capability**: SystemCapability.Utils.Lang
672
673**Return value**
674
675| Type| Description                         |
676| ---- | ----------------------------- |
677| string  | Last segment of the URI.|
678
679**Example**
680
681```ts
682const uriInstance = new uri.URI("content://com.test.provider/files/image.jpg");
683console.info(uriInstance.getLastSegment()); // image.jpg
684```
685
686### getSegment<sup>12+</sup>
687
688getSegment(): string[]
689
690Obtains all segments of this URI.
691
692**Atomic service API**: This API can be used in atomic services since API version 12.
693
694**System capability**: SystemCapability.Utils.Lang
695
696**Return value**
697
698| Type    | Description                       |
699| -------- | --------------------------- |
700| string[] | All segments of this URI.|
701
702**Example**
703
704```ts
705const uriInstance = new uri.URI("http://www.test.com/path/to/image.jpg");
706console.info(uriInstance.getSegment().toString()); // path,to,image.jpg
707```
708
709### createFromParts<sup>12+</sup>
710
711createFromParts(scheme: string, ssp: string, fragment: string): URI
712
713Creates a URI based on the provided scheme, scheme-specific-part, and fragment components.
714
715**Atomic service API**: This API can be used in atomic services since API version 12.
716
717**System capability**: SystemCapability.Utils.Lang
718
719**Parameters**
720
721| Name  | Type  | Mandatory| Description                           |
722| -------- | ------ | ---- | ------------------------------- |
723| scheme   | string | Yes  | Scheme of the URI.              |
724| ssp      | string | Yes  | Scheme-specific-part of the URI.|
725| fragment | string | Yes  | Fragment of this URI. The fragment component is the part following the number sign (#).            |
726
727**Return value**
728
729| Type| Description                                             |
730| ---- | ------------------------------------------------- |
731| [URI](#uri)  | URI object obtained.|
732
733**Error codes**
734
735For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
736
737| ID| Error Message|
738| -------- | -------- |
739| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
740
741**Example**
742
743```ts
744const uriInstance = uri.URI.createFromParts("mailto", "no body", "top");
745console.info(uriInstance.toString()); // mailto:no%20body#top
746```
747
748### equals<sup>(deprecated)</sup>
749
750equals(other: URI): boolean
751
752Checks whether this URI is the same as another URI object.
753
754> **NOTE**
755>
756> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [equalsTo<sup>9+</sup>](#equalsto9) instead.
757
758**System capability**: SystemCapability.Utils.Lang
759
760**Parameters**
761
762| Name| Type| Mandatory| Description|
763| -------- | -------- | -------- | -------- |
764| other | [URI](#uri) | Yes| URI object to compare.|
765
766**Return value**
767
768| Type| Description|
769| -------- | -------- |
770| boolean | Returns **true** if the two URIs are the same; returns **false** otherwise.|
771
772**Example**
773
774```ts
775const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
776const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
777uriInstance.equals(uriInstance1); // true
778```
779