• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
271> - 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.
272>
273> 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.
274
275
276**Atomic service API**: This API can be used in atomic services since API version 11.
277
278**System capability**: SystemCapability.Utils.Lang
279
280**Return value**
281
282| Type| Description|
283| -------- | -------- |
284| [URI](#uri) | URI with the normalized path.|
285
286**Example**
287
288```ts
289const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080/path/path1/../path2/./path3?query=pppppp');
290console.info(uriInstance.path); // /path/path1/../path2/./path3
291// 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.
292let uriInstance1 = uriInstance.normalize();
293console.info(uriInstance1.path); // /path/path2/path3
294let uri1 = new uri.URI('http://www.test.com/../../patch/path1/../path2/path3/./path4/../');
295console.log(uri1.path); // /../../patch/path1/../path2/path3/./path4/../
296// 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.
297let uri2 = uri1.normalize();
298console.log(uri2.path); // /../../patch/path2/path3
299```
300
301### checkRelative<sup>12+</sup>
302
303checkRelative(): boolean
304
305Checks whether this URI is a relative URI. A relative URI does not contain the scheme component.
306
307**Atomic service API**: This API can be used in atomic services since API version 12.
308
309**System capability**: SystemCapability.Utils.Lang
310
311**Return value**
312
313| Type   | Description                                      |
314| ------- | ------------------------------------------ |
315| boolean | **true**: The URI is a relative URI.<br>**false**: The URI is not a relative URI.|
316
317**Example**
318
319```ts
320const uriInstance = new uri.URI("https://username:password@www.qwer.com:8080?query=p");
321console.info(`${uriInstance.checkRelative()}`); // false
322const uriInstance1 = new uri.URI("/images/pic.jpg");
323console.info(`${uriInstance1.checkRelative()}`); // true
324```
325
326### checkOpaque<sup>12+</sup>
327
328checkOpaque(): boolean
329
330Checks whether this URI is an opaque URI. The URI that does not start with a slash (/) is an opaque URI.
331
332**Atomic service API**: This API can be used in atomic services since API version 12.
333
334**System capability**: SystemCapability.Utils.Lang
335
336**Return value**
337
338| Type   | Description                                          |
339| ------- | ---------------------------------------------- |
340| boolean | **true**: The URI is an opaque URI.<br>**false**: The URI is not an opaque URI.|
341
342**Example**
343
344```ts
345const uriInstance = new uri.URI("http://www.test.com/images/pic.jpg");
346console.info(`${uriInstance.checkOpaque()}`); // false
347const uriInstance1 = new uri.URI("mailto:user@example.com");
348console.info(`${uriInstance1.checkOpaque()}`); // true
349```
350
351### checkHierarchical<sup>12+</sup>
352
353checkHierarchical(): boolean
354
355Checks 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.
356
357**Atomic service API**: This API can be used in atomic services since API version 12.
358
359**System capability**: SystemCapability.Utils.Lang
360
361**Return value**
362
363| Type   | Description                                        |
364| ------- | -------------------------------------------- |
365| boolean | **true**: The URI is a hierarchical URI.<br>**false**: The URI is not a hierarchical URI.|
366
367**Example**
368
369```ts
370const uriInstance = new uri.URI("http://www.test.com/images/pic.jpg");
371console.info(`${uriInstance.checkHierarchical()}`); // true
372const uriInstance1 = new uri.URI("mailto:user@example.com");
373console.info(`${uriInstance1.checkHierarchical()}`); // false
374```
375
376### getQueryValue<sup>12+</sup>
377
378getQueryValue(key:string): string
379
380Obtains 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.
381
382The 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.
383
384**Atomic service API**: This API can be used in atomic services since API version 12.
385
386**System capability**: SystemCapability.Utils.Lang
387
388**Parameters**
389
390| Name| Type  | Mandatory| Description                   |
391| ------ | ------ | ---- | ----------------------- |
392| key    | string | Yes  | Key of the URI query parameter.|
393
394**Return value**
395
396| Type  | Description                         |
397| ------ | ----------------------------- |
398| string | First value obtained. If no value is found, a null object is returned.|
399
400**Error codes**
401
402For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
403
404| ID| Error Message|
405| -------- | -------- |
406| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
407
408**Example**
409
410```ts
411const uriInstance = new uri.URI("https://www.com?param1=value1&param2=value2");
412console.info(uriInstance.getQueryValue("param1")); // value1
413let uriInstance1 = new uri.URI('https://www.zyy.ss?sa%3D=po%7E');
414console.info(uriInstance1.getQueryValue('sa=')) // po~
415console.info(uriInstance1.getQueryValue('abc')) // null
416```
417
418### addQueryValue<sup>12+</sup>
419
420addQueryValue(key:string, value:string): URI
421
422Adds a query parameter to this URI to create a new URI, while keeping the existing URI unchanged.
423
424**Atomic service API**: This API can be used in atomic services since API version 12.
425
426**System capability**: SystemCapability.Utils.Lang
427
428**Parameters**
429
430| Name| Type  | Mandatory| Description                    |
431| ------ | ------ | ---- | ------------------------ |
432| key    | string | Yes  | Key of the query parameter.|
433| value  | string | Yes  | Value of the query parameter.  |
434
435**Return value**
436
437| Type| Description                            |
438| ---- | -------------------------------- |
439| [URI](#uri)  | URI object with the query parameter.|
440
441**Error codes**
442
443For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
444
445| ID| Error Message|
446| -------- | -------- |
447| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
448
449**Example**
450
451```ts
452const uriInstance = new uri.URI("https://www.test.com");
453const newRoute = uriInstance.addQueryValue("param1", "hello world");
454console.info(newRoute.toString()); // https://www.test.com?param1=hello%20world
455```
456
457### addSegment<sup>12+</sup>
458
459addSegment(pathSegment:string): URI
460
461Encodes 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.
462
463**Atomic service API**: This API can be used in atomic services since API version 12.
464
465**System capability**: SystemCapability.Utils.Lang
466
467**Parameters**
468
469| Name     | Type  | Mandatory| Description              |
470| ----------- | ------ | ---- | ------------------ |
471| pathSegment | string | Yes  | Field to be appended to the path component.|
472
473**Return value**
474
475| Type| Description                            |
476| ---- | -------------------------------- |
477| [URI](#uri)  | URI object with the appended field.|
478
479**Error codes**
480
481For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
482
483| ID| Error Message|
484| -------- | -------- |
485| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
486
487**Example**
488
489```ts
490const uriInstance = new uri.URI("http://www.test.com");
491const newRoute = uriInstance.addSegment("my image.jpg");
492console.info(newRoute.toString()); // http://www.test.com/my%20image.jpg
493```
494
495### addEncodedSegment<sup>12+</sup>
496
497addEncodedSegment(pathSegment:string): URI
498
499Appends 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.
500
501**Atomic service API**: This API can be used in atomic services since API version 12.
502
503**System capability**: SystemCapability.Utils.Lang
504
505**Parameters**
506
507| Name     | Type  | Mandatory| Description              |
508| ----------- | ------ | ---- | ------------------ |
509| pathSegment | string | Yes  | Encoded field to be appended to the path component.|
510
511**Return value**
512
513| Type| Description                            |
514| ---- | -------------------------------- |
515| [URI](#uri)  | URI object with the appended field.|
516
517**Error codes**
518
519For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
520
521| ID| Error Message|
522| -------- | -------- |
523| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
524
525**Example**
526
527```ts
528const uriInstance = new uri.URI("http://www.test.com");
529const newRoute = uriInstance.addEncodedSegment("my%20image.jpg");
530console.info(newRoute.toString()); // http://www.test.com/my%20image.jpg
531```
532
533### getQueryNames<sup>12+</sup>
534
535getQueryNames(): string[]
536
537Obtains 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.
538
539**Atomic service API**: This API can be used in atomic services since API version 12.
540
541**System capability**: SystemCapability.Utils.Lang
542
543**Return value**
544
545| Type       | Description                               |
546| ----------- | ----------------------------------- |
547| string[] | Non-repeated keys in the query component.|
548
549**Example**
550
551```ts
552const uriInstance = new uri.URI("https://www.test.com?param1=value1&param2=value2");
553const paramNames = uriInstance.getQueryNames();
554console.info(Array.from(paramNames).toString()); // param1,param2
555```
556
557### getQueryValues<sup>12+</sup>
558
559getQueryValues(key:string): string[]
560
561Obtains 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.
562
563The 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.
564
565**Atomic service API**: This API can be used in atomic services since API version 12.
566
567**System capability**: SystemCapability.Utils.Lang
568
569**Parameters**
570
571| Name| Type  | Mandatory| Description                   |
572| ------ | ------ | ---- | ----------------------- |
573| key    | string | Yes  | Key of the URI query parameter.|
574
575**Return value**
576
577| Type    | Description                               |
578| -------- | ----------------------------------- |
579| string[] | Array of values obtained. If no value is found, an empty string array [] is returned.|
580
581**Error codes**
582
583For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
584
585| ID| Error Message|
586| -------- | -------- |
587| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
588
589**Example**
590
591```ts
592const uriInstance = new uri.URI("https://www.test.com/search?query=name&query=my");
593console.info(uriInstance.getQueryValues("query").toString()); // name,my
594console.info(JSON.stringify(uriInstance.getQueryValues("abc"))); // []
595```
596
597### getBooleanQueryValue<sup>12+</sup>
598
599getBooleanQueryValue(key:string,defaultValue:boolean): boolean
600
601Obtains the value of the Boolean type of a query parameter in this URI.
602
603**Atomic service API**: This API can be used in atomic services since API version 12.
604
605**System capability**: SystemCapability.Utils.Lang
606
607**Parameters**
608
609| Name      | Type   | Mandatory| Description                                 |
610| ------------ | ------- | ---- | ------------------------------------- |
611| key          | string  | Yes  | Name of the query parameter.              |
612| defaultValue | boolean | Yes  | Default value returned when the query parameter does not contain the specified key.|
613
614**Return value**
615
616| Type   | Description                                                                  |
617| ------- | ---------------------------------------------------------------------- |
618| 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.|
619
620**Error codes**
621
622For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
623
624| ID| Error Message|
625| -------- | -------- |
626| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
627
628**Example**
629
630```ts
631const uriInstance = new uri.URI("https://www.test.com/search?active=true");
632console.info(`${uriInstance.getBooleanQueryValue("active", false)}`); // true
633const uriInstance1 = new uri.URI("https://www.test.com/search");
634console.info(`${uriInstance1.getBooleanQueryValue("active", false)}`); // false
635const uriInstance2 = new uri.URI("https://www.test.com/search?active=aa&active=false");
636console.info(`${uriInstance2.getBooleanQueryValue("active", false)}`); // true
637const uriInstance3 = new uri.URI("https://www.test.com/search?active=0");
638console.info(`${uriInstance3.getBooleanQueryValue("active", true)}`); // false
639const uriInstance4 = new uri.URI("https://www.test.com/search");
640console.info(`${uriInstance4.getBooleanQueryValue("active", true)}`); // true
641```
642
643### clearQuery<sup>12+</sup>
644
645clearQuery(): URI
646
647Clears the query component of this URI to create a new URI, while keeping the existing URI object unchanged.
648
649**Atomic service API**: This API can be used in atomic services since API version 12.
650
651**System capability**: SystemCapability.Utils.Lang
652
653**Return value**
654
655| Type| Description                                 |
656| ---- | ------------------------------------- |
657| [URI](#uri)  | URI object whose query component has been cleared.|
658
659**Example**
660
661```ts
662const uriInstance = new uri.URI("https://www.test.com?param1=value1");
663console.info(uriInstance.clearQuery().toString()); // https://www.test.com
664```
665
666### getLastSegment<sup>12+</sup>
667
668getLastSegment(): string
669
670Obtains 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.
671
672**Atomic service API**: This API can be used in atomic services since API version 12.
673
674**System capability**: SystemCapability.Utils.Lang
675
676**Return value**
677
678| Type| Description                         |
679| ---- | ----------------------------- |
680| string  | Last segment of the URI.|
681
682**Example**
683
684```ts
685const uriInstance = new uri.URI("content://com.test.provider/files/image.jpg");
686console.info(uriInstance.getLastSegment()); // image.jpg
687```
688
689### getSegment<sup>12+</sup>
690
691getSegment(): string[]
692
693Obtains all segments of this URI.
694
695**Atomic service API**: This API can be used in atomic services since API version 12.
696
697**System capability**: SystemCapability.Utils.Lang
698
699**Return value**
700
701| Type    | Description                       |
702| -------- | --------------------------- |
703| string[] | All segments of this URI.|
704
705**Example**
706
707```ts
708const uriInstance = new uri.URI("http://www.test.com/path/to/image.jpg");
709console.info(uriInstance.getSegment().toString()); // path,to,image.jpg
710```
711
712### createFromParts<sup>12+</sup>
713
714createFromParts(scheme: string, ssp: string, fragment: string): URI
715
716Creates a URI based on the provided scheme, scheme-specific-part, and fragment components.
717
718**Atomic service API**: This API can be used in atomic services since API version 12.
719
720**System capability**: SystemCapability.Utils.Lang
721
722**Parameters**
723
724| Name  | Type  | Mandatory| Description                           |
725| -------- | ------ | ---- | ------------------------------- |
726| scheme   | string | Yes  | Scheme of the URI.              |
727| ssp      | string | Yes  | Scheme-specific-part of the URI.|
728| fragment | string | Yes  | Fragment of this URI. The fragment component is the part following the number sign (#).            |
729
730**Return value**
731
732| Type| Description                                             |
733| ---- | ------------------------------------------------- |
734| [URI](#uri)  | URI object obtained.|
735
736**Error codes**
737
738For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
739
740| ID| Error Message|
741| -------- | -------- |
742| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
743
744**Example**
745
746```ts
747const uriInstance = uri.URI.createFromParts("mailto", "no body", "top");
748console.info(uriInstance.toString()); // mailto:no%20body#top
749```
750
751### equals<sup>(deprecated)</sup>
752
753equals(other: URI): boolean
754
755Checks whether this URI is the same as another URI object.
756
757> **NOTE**
758>
759> 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.
760
761**System capability**: SystemCapability.Utils.Lang
762
763**Parameters**
764
765| Name| Type| Mandatory| Description|
766| -------- | -------- | -------- | -------- |
767| other | [URI](#uri) | Yes| URI object to compare.|
768
769**Return value**
770
771| Type| Description|
772| -------- | -------- |
773| boolean | Returns **true** if the two URIs are the same; returns **false** otherwise.|
774
775**Example**
776
777```ts
778const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
779const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
780uriInstance.equals(uriInstance1); // true
781```
782