• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.uri (URI String Parsing)
2
3> **NOTE**
4>
5> 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.
6
7
8## Modules to Import
9
10```js
11import uri from '@ohos.uri'
12```
13
14## URI
15
16### Attributes
17
18**System capability**: SystemCapability.Utils.Lang
19
20| Name| Type| Readable| Writable| Description|
21| -------- | -------- | -------- | -------- | -------- |
22| scheme | string | Yes| No| Scheme in the URI.|
23| userInfo | string | Yes| No| User information in the URI.|
24| host | string | Yes| No| Host name (without the port number) in the URI.|
25| port | string | Yes| No| Port number in the URI.|
26| path | string | Yes| No| Path in the URI.|
27| query | string | Yes| No| Query part in the URI.|
28| fragment | string | Yes| No| Fragment part in the URI.|
29| authority | string | Yes| No| Authority part in the URI.|
30| ssp | string | Yes| No| Scheme-specific part in the URI.|
31
32### Naming Rules
33
34Naming format:
35
36A standard URI consists of the following parts:
37[scheme:]scheme-specific-part[#fragment]
38- scheme: scheme component. Set this parameter as required. Example values: **http**, **https**, **ftp**, **datashare**, and **dataability**.
39- scheme-specific-part: specific part of the URI decoding scheme. The value consists of [//][authority][path][?query]. Set this parameter as required.
40    - authority: decoding authority component of the URI. The value consists of [userinfo@]host[:port]. Set this parameter as required.
41        - userinfo: user information. Set this parameter as required.
42        - host: host name of the server. This parameter is mandatory when authority exists.
43        - port: port number of the server. Set this parameter as required.
44    - path: path information. Set this parameter as required.
45    - query: query component. Set this parameter as required.
46- fragment: fragment component. Set this parameter as required.
47
48**Example URIs**
49
50```js
51const result1 = new uri.URI("ftp://ftp.aaa.bbb.ccc/dddd/eee.txt");
52console.log(result1.host) // ftp.aaa.bbb.ccc
53console.log(result1.fragment) // null
54console.log(result1.path) // /dddd/eee.txt
55console.log(result1.scheme) // ftp
56console.log(result1.userInfo) // null
57console.log(result1.port) // -1
58console.log(result1.query) // null
59
60const result2 = new uri.URI("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#fragment");
61console.log(result2.host) // spinaltap.micro.umn.edu
62console.log(result2.fragment) // fragment
63console.log(result2.path) // /dddd/eee.txt
64console.log(result2.scheme) // ftp
65console.log(result2.userInfo) // null
66console.log(result2.port) //-1
67console.log(result2.query) // null
68
69const result3 = new uri.URI("datashare:///com.samples.datasharetest.DataShare/DB00/TBL00");
70console.log(result3.host) // null
71console.log(result3.fragment) // null
72console.log(result3.path) // /com.samples.datasharetest.DataShare/DB00/TBL00
73console.log(result3.scheme) // datashare
74console.log(result3.userInfo) // null
75console.log(result3.port) // -1
76console.log(result3.query) // null
77
78const result4 = new uri.URI("https://username:password@host:8080/directory/file?foo=1&bar=2#fragment");
79console.log(result4.host) // host
80console.log(result4.fragment) // fragment
81console.log(result4.path) // /directory/file
82console.log(result4.scheme) // https
83console.log(result4.userInfo) // username:password
84console.log(result4.port) // 8080
85console.log(result4.query) // foo=1&bar=2
86
87const result5 = new uri.URI("dataability:///com.example.DataAbility");
88console.log(result5.host) // null
89console.log(result5.fragment) // null
90console.log(result5.path) // /com.example.DataAbility:
91console.log(result5.scheme) // dataability
92console.log(result5.userInfo) // null
93console.log(result5.port) // -1
94console.log(result5.query) // null
95```
96
97### constructor
98
99constructor(uri: string)
100
101A constructor used to create a URI instance.
102
103**System capability**: SystemCapability.Utils.Lang
104
105**Parameters**
106
107| Name| Type| Mandatory| Description|
108| -------- | -------- | -------- | -------- |
109| uri | string | Yes| Input object.|
110
111**Error codes**
112
113For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
114
115| ID| Error Message|
116| -------- | -------- |
117| 10200002 | Invalid uri string. |
118
119**Example**
120
121```js
122let mm = 'https://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
123new uri.URI(mm); // Output 'https://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
124```
125```js
126new uri.URI('https://username:password@host:8080'); // Output 'https://username:password@host:8080';
127```
128
129
130### toString
131
132toString(): string
133
134**System capability**: SystemCapability.Utils.Lang
135
136Obtains the query string applicable to this URI.
137
138**Return value**
139
140| Type| Description|
141| -------- | -------- |
142| string | Website address in a serialized string.|
143
144**Example**
145
146```js
147const result = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
148let result1 = result.toString();
149```
150
151
152### equals<sup>(deprecated)</sup>
153
154equals(other: URI): boolean
155
156Checks whether this URI is the same as another URI object.
157
158> **NOTE**
159>
160> 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.
161
162**System capability**: SystemCapability.Utils.Lang
163
164**Parameters**
165
166| Name| Type| Mandatory| Description|
167| -------- | -------- | -------- | -------- |
168| other | [URI](#uri) | Yes| URI object to compare.|
169
170**Return value**
171
172| Type| Description|
173| -------- | -------- |
174| boolean | Returns **true** if the two URIs are the same; returns **false** otherwise.|
175
176**Example**
177
178```js
179const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
180const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
181uriInstance.equals(uriInstance1);
182```
183### equalsTo<sup>9+</sup>
184
185equalsTo(other: URI): boolean
186
187Checks whether this URI is the same as another URI object.
188
189**System capability**: SystemCapability.Utils.Lang
190
191**Parameters**
192
193| Name| Type| Mandatory| Description|
194| -------- | -------- | -------- | -------- |
195| other | [URI](#uri) | Yes| URI object to compare.|
196
197**Return value**
198
199| Type| Description|
200| -------- | -------- |
201| boolean | Returns **true** if the two URIs are the same; returns **false** otherwise.|
202
203**Example**
204
205```js
206const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
207const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
208let result = uriInstance.equalsTo(uriInstance1);
209```
210
211### checkIsAbsolute
212
213checkIsAbsolute(): boolean
214
215Checks whether this URI is an absolute URI (whether the scheme component is defined).
216
217**System capability**: SystemCapability.Utils.Lang
218
219**Return value**
220
221| Type| Description|
222| -------- | -------- |
223| boolean | If the URI is an absolute URI, **true** is returned. Otherwise, **false** is returned.|
224
225**Example**
226
227```js
228const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080?query=pppppp');
229console.log(`${uriInstance.checkIsAbsolute()}`); // true
230const uriInstance1 = new uri.URI('xxx.com/suppliers.htm');
231console.log(`${uriInstance1.checkIsAbsolute()}`); // false
232```
233
234
235### normalize
236
237normalize(): URI
238
239Normalizes the path of this URI.
240
241**System capability**: SystemCapability.Utils.Lang
242
243**Return value**
244
245| Type| Description|
246| -------- | -------- |
247| URI | URI with the normalized path.|
248
249**Example**
250
251```js
252const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080/path/path1/../path2/./path3?query=pppppp');
253console.log(uriInstance.path); // /path/path1/../path2/./path3
254let uriInstance1 = uriInstance.normalize();
255console.log(uriInstance1.path); // /path/path2/path3
256```
257