js-apis-uri.md 7.7 KB
Newer Older
1
# @ohos.uri (URI String Parsing)
Z
zengyawen 已提交
2

W
wusongqing 已提交
3 4
> **NOTE**
>
W
wusongqing 已提交
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.
Z
zengyawen 已提交
6

W
wusongqing 已提交
7 8

## Modules to Import
Z
zengyawen 已提交
9

W
wusongqing 已提交
10
```js
Z
zengyawen 已提交
11 12 13
import uri from '@ohos.uri'  
```

W
wusongqing 已提交
14 15 16 17
## URI

### Attributes

W
wusongqing 已提交
18 19
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
20 21 22 23 24 25 26 27 28 29 30 31
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| scheme | string | Yes| No| Scheme in the URI.|
| userInfo | string | Yes| No| User information in the URI.|
| host | string | Yes| No| Host name (without the port number) in the URI.|
| port | string | Yes| No| Port number in the URI.|
| path | string | Yes| No| Path in the URI.|
| query | string | Yes| No| Query part in the URI.|
| fragment | string | Yes| No| Fragment part in the URI.|
| authority | string | Yes| No| Authority part in the URI.|
| ssp | string | Yes| No| Scheme-specific part in the URI.|

32 33 34 35 36 37
### Naming Rules

Naming format:

A standard URI consists of the following parts:
[scheme:]scheme-specific-part[#fragment]
38
- scheme: scheme component. Set this parameter as required. Example values: **http**, **https**, **ftp**, **datashare**, and **dataability**.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
- scheme-specific-part: specific part of the URI decoding scheme. The value consists of [//][authority][path][?query]. Set this parameter as required.
    - authority: decoding authority component of the URI. The value consists of [userinfo@]host[:port]. Set this parameter as required.
        - userinfo: user information. Set this parameter as required.
        - host: host name of the server. This parameter is mandatory when authority exists.
        - port: port number of the server. Set this parameter as required.
    - path: path information. Set this parameter as required.
    - query: query component. Set this parameter as required.
- fragment: fragment component. Set this parameter as required.

**Example URIs**

```js
const result1 = new uri.URI("ftp://ftp.aaa.bbb.ccc/dddd/eee.txt");
console.log(result1.host) // ftp.aaa.bbb.ccc
console.log(result1.fragment) // null
console.log(result1.path) // /dddd/eee.txt
console.log(result1.scheme) // ftp
console.log(result1.userInfo) // null
console.log(result1.port) // -1
console.log(result1.query) // null

const result2 = new uri.URI("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#fragment");
console.log(result2.host) // spinaltap.micro.umn.edu
console.log(result2.fragment) // fragment
console.log(result2.path) // /dddd/eee.txt
console.log(result2.scheme) // ftp
console.log(result2.userInfo) // null
console.log(result2.port) //-1
console.log(result2.query) // null

const result3 = new uri.URI("datashare:///com.samples.datasharetest.DataShare/DB00/TBL00");
console.log(result3.host) // null
console.log(result3.fragment) // null
console.log(result3.path) // /com.samples.datasharetest.DataShare/DB00/TBL00
console.log(result3.scheme) // datashare
console.log(result3.userInfo) // null
console.log(result3.port) // -1
console.log(result3.query) // null

const result4 = new uri.URI("https://username:password@host:8080/directory/file?foo=1&bar=2#fragment");
console.log(result4.host) // host
console.log(result4.fragment) // fragment
console.log(result4.path) // /directory/file
console.log(result4.scheme) // https
console.log(result4.userInfo) // username:password
console.log(result4.port) // 8080
console.log(result4.query) // foo=1&bar=2

const result5 = new uri.URI("dataability:///com.example.DataAbility");
console.log(result5.host) // null
console.log(result5.fragment) // null
console.log(result5.path) // /com.example.DataAbility:
console.log(result5.scheme) // dataability
console.log(result5.userInfo) // null
console.log(result5.port) // -1
console.log(result5.query) // null
```
W
wusongqing 已提交
96 97 98 99

### constructor

constructor(uri: string)
Z
zengyawen 已提交
100 101 102

A constructor used to create a URI instance.

W
wusongqing 已提交
103 104
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
105
**Parameters**
Z
zengyawen 已提交
106

107 108 109 110 111 112 113 114 115 116 117
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| uri | string | Yes| Input object.|

**Error codes**

For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).

| ID| Error Message|
| -------- | -------- |
| 10200002 | Invalid uri string. |
Z
zengyawen 已提交
118

W
wusongqing 已提交
119
**Example**
Z
zengyawen 已提交
120

121
```js
122 123
let mm = 'https://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
new uri.URI(mm); // Output 'https://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
W
wusongqing 已提交
124
```
125
```js
126
new uri.URI('https://username:password@host:8080'); // Output 'https://username:password@host:8080';
W
wusongqing 已提交
127 128 129 130
```


### toString
Z
zengyawen 已提交
131

W
wusongqing 已提交
132
toString(): string
Z
zengyawen 已提交
133

W
wusongqing 已提交
134 135
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
136
Obtains the query string applicable to this URI.
W
wusongqing 已提交
137 138 139

**Return value**

140
| Type| Description|
W
wusongqing 已提交
141 142
| -------- | -------- |
| string | Website address in a serialized string.|
Z
zengyawen 已提交
143

W
wusongqing 已提交
144
**Example**
Z
zengyawen 已提交
145

146
```js
147
const result = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
S
shikai-123 已提交
148
result.toString()
W
wusongqing 已提交
149
```
Z
zengyawen 已提交
150 151


G
Gloria 已提交
152
### equals<sup>(deprecated)</sup>
W
wusongqing 已提交
153 154

equals(other: URI): boolean
Z
zengyawen 已提交
155 156 157

Checks whether this URI is the same as another URI object.

158 159 160 161
> **NOTE**
>
> 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.

W
wusongqing 已提交
162 163
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
164
**Parameters**
Z
zengyawen 已提交
165

166
| Name| Type| Mandatory| Description|
W
wusongqing 已提交
167 168 169 170 171
| -------- | -------- | -------- | -------- |
| other | [URI](#uri) | Yes| URI object to compare.|

**Return value**

172
| Type| Description|
W
wusongqing 已提交
173 174 175 176 177
| -------- | -------- |
| boolean | Returns **true** if the two URIs are the same; returns **false** otherwise.|

**Example**

178
```js
179 180
const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
W
wusongqing 已提交
181 182
uriInstance.equals(uriInstance1);
```
G
Gloria 已提交
183 184 185 186 187 188 189 190 191 192
### equalsTo<sup>9+</sup>

equalsTo(other: URI): boolean

Checks whether this URI is the same as another URI object.

**System capability**: SystemCapability.Utils.Lang

**Parameters**

193
| Name| Type| Mandatory| Description|
G
Gloria 已提交
194 195 196 197 198
| -------- | -------- | -------- | -------- |
| other | [URI](#uri) | Yes| URI object to compare.|

**Return value**

199
| Type| Description|
G
Gloria 已提交
200 201 202 203 204 205
| -------- | -------- |
| boolean | Returns **true** if the two URIs are the same; returns **false** otherwise.|

**Example**

```js
206 207
const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
G
Gloria 已提交
208 209
uriInstance.equalsTo(uriInstance1);
```
W
wusongqing 已提交
210 211 212 213

### checkIsAbsolute

checkIsAbsolute(): boolean
Z
zengyawen 已提交
214

W
wusongqing 已提交
215
Checks whether this URI is an absolute URI (whether the scheme component is defined).
Z
zengyawen 已提交
216

W
wusongqing 已提交
217 218
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
219
**Return value**
Z
zengyawen 已提交
220

221
| Type| Description|
W
wusongqing 已提交
222
| -------- | -------- |
223
| boolean | If the URI is an absolute URI, **true** is returned. Otherwise, **false** is returned.|
Z
zengyawen 已提交
224

W
wusongqing 已提交
225
**Example**
Z
zengyawen 已提交
226

227
```js
228
const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080?query=pppppp');
229 230 231
console.log(uriInstance.checkIsAbsolute()); // true
const uriInstance1 = new uri.URI('xxx.com/suppliers.htm');
console.log(uriInstance1.checkIsAbsolute()); // false
W
wusongqing 已提交
232 233 234 235 236 237
```


### normalize

normalize(): URI
Z
zengyawen 已提交
238

W
wusongqing 已提交
239 240
Normalizes the path of this URI.

W
wusongqing 已提交
241 242
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
243 244
**Return value**

245
| Type| Description|
W
wusongqing 已提交
246 247
| -------- | -------- |
| URI | URI with the normalized path.|
Z
zengyawen 已提交
248

W
wusongqing 已提交
249
**Example**
250

251
```js
252
const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080/path/path1/../path2/./path3?query=pppppp');
253
console.log(uriInstance.path); // /path/path1/../path2/./path3
W
wusongqing 已提交
254
let uriInstance1 = uriInstance.normalize();
255
console.log(uriInstance1.path); // /path/path2/path3
W
wusongqing 已提交
256
```