js-apis-system-request.md 8.3 KB
Newer Older
E
esterzhou 已提交
1
# @system.request
Z
zengyawen 已提交
2

E
esterzhou 已提交
3 4 5 6
The **system.request** module provides applications with basic upload and download capabilities.

> **NOTE**
> - The APIs of this module are deprecated since API version 9. You are advised to use [`@ohos.request`](js-apis-request.md) instead.
Z
zengyawen 已提交
7
> 
E
esterzhou 已提交
8
> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
9 10 11 12 13 14 15 16 17


## Modules to Import


```
import request from '@system.request';
```

E
esterzhou 已提交
18
## request.upload
Z
zengyawen 已提交
19

E
esterzhou 已提交
20
upload(options: UploadRequestOptions): void
Z
zengyawen 已提交
21

E
esterzhou 已提交
22
Uploads a file. This API returns no value.
Z
zengyawen 已提交
23

E
esterzhou 已提交
24
**System capability**: SystemCapability.MiscServices.Upload
Z
zengyawen 已提交
25

E
esterzhou 已提交
26
**Parameters**
Z
zengyawen 已提交
27

E
esterzhou 已提交
28 29 30
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | options | [UploadRequestOptions](#uploadrequestoptions) | Yes| Upload configurations.|
Z
zengyawen 已提交
31

E
esterzhou 已提交
32
**Example**
Z
zengyawen 已提交
33

E
esterzhou 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  ```js
  let uploadRequestOptions = {
    url: 'http://www.path.com',
    method: 'POST',
    files: [{ filename: "test", name: "test", uri: "internal://cache/test.jpg", type: "jpg" }],
    data: [{ name: "name123", value: "123" }],
    success: function(data) {
      console.info(' upload success, code:' + JSON.stringify(data));
    },
    fail: function(data, code) {
      console.info(' upload fail data: ' + data + 'code: ' + code);
    },
    complete: function (){
      console.info(' upload complete');
    }
  }
  try {
    request.upload(uploadRequestOptions);
    console.info('upload start ');
  } catch(err) {
    console.info(' upload err:' + err);
  }
  ```
Z
zengyawen 已提交
57 58


E
esterzhou 已提交
59
## UploadRequestOptions
Z
zengyawen 已提交
60

E
esterzhou 已提交
61
**System capability**: SystemCapability.MiscServices.Upload
Z
zengyawen 已提交
62 63


E
esterzhou 已提交
64 65 66 67 68 69 70 71 72 73
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | url | string | Yes| URL of the upload server.|
  | data | Array<[RequestData](#requestdata)> | No| Form data in the request body.|
  | files | Array<[RequestFile](#requestfile)> | Yes| List of files to upload, which is submitted through **multipart/form-data**.|
  | header | Object | No| Request header.|
  | method | string | No| Request method, which can be **'POST'** or **'PUT'**. The default value is **POST**.|
  | success | Function | No| Called when API call is successful.|
  | fail | Function | No| Called when API call has failed.|
  | complete | Function | No| Called when API call is complete.|
Z
zengyawen 已提交
74

E
esterzhou 已提交
75 76 77 78
**success parameter**
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | data | [UploadResponse](#uploadresponse) | Yes| Information returned when the upload task is successful.|
Z
zengyawen 已提交
79

E
esterzhou 已提交
80 81 82 83 84
**fail parameters**
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | data | any | Yes| Header information returned when the upload task fails.|
  | code | number | Yes| HTTP status code returned when the upload task fails.|
E
add doc  
ester.zhou 已提交
85

Z
zengyawen 已提交
86 87


E
esterzhou 已提交
88
## UploadResponse
Z
zengyawen 已提交
89

E
esterzhou 已提交
90
**System capability**: SystemCapability.MiscServices.Upload
Z
zengyawen 已提交
91

E
esterzhou 已提交
92 93 94 95 96
  | Name| Type| Description|
  | -------- | -------- | -------- |
  | code | number | HTTP status code returned by the server.|
  | data | string | Content returned by the server. The value type is determined by the type in the returned headers.|
  | headers | Object | Headers returned by the server.|
Z
zengyawen 已提交
97 98


E
esterzhou 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
## RequestFile

**System capability**: SystemCapability.MiscServices.Upload

  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | filename | string | No| File name in the header when **multipart** is used.|
  | name | string | No| Name of a form item when **multipart** is used. The default value is **file**.|
  | uri | string | Yes| Local path for storing files.|
  | type | string | No| Type of the file content. By default, the type is obtained based on the extension of the file name or URI.|


## RequestData

**System capability**: SystemCapability.MiscServices.Upload
Z
zengyawen 已提交
114

E
esterzhou 已提交
115 116 117 118
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | name | string | Yes| Name of the form element.|
  | value | string | Yes| Value of the form element.|
Z
zengyawen 已提交
119 120 121



E
esterzhou 已提交
122 123 124 125 126 127 128 129 130
## request.download

download(options: DownloadRequestOptions): void

Downloads a file. This API returns no value.

**System capability**: SystemCapability.MiscServices.Download

**Parameters**
Z
zengyawen 已提交
131

E
esterzhou 已提交
132 133 134
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | options | [DownloadRequestOptions](#downloadrequestoptions) | Yes| Download configurations.|
Z
zengyawen 已提交
135 136 137

**Example**

E
esterzhou 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  ```js
  let downloadRequestOptions = {
    url: 'http://www.path.com',
    filename: 'requestSystenTest',
    header: '',
    description: 'this is requeSystem download response',
    success: function(data) {
      console.info(' download success, code:' + JSON.stringify(data));
    },
    fail: function(data, code) {
      console.info(' download fail data: ' + data + 'code: ' + code);
    },
    complete: function (){
      console.info(' download complete');
    }
Z
zengyawen 已提交
153
  }
E
esterzhou 已提交
154 155 156 157 158 159 160
  try {
    request.download(downloadRequestOptions);
    console.info('download start ');
  } catch(err) {
    console.info(' download err:' + err);
  }
  ```
Z
zengyawen 已提交
161 162


E
esterzhou 已提交
163
## DownloadRequestOptions
Z
zengyawen 已提交
164

E
esterzhou 已提交
165
**System capability**: SystemCapability.MiscServices.Download
Z
zengyawen 已提交
166

E
esterzhou 已提交
167 168 169 170 171 172 173 174 175
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | url | string | Yes| Resource URL.|
  | filename | string | No| Name of the file to download. The value is obtained from the current request or resource URL by default.|
  | header | Object | No| Request header.|
  | description | string | No| Download description. The default value is the file name.|
  | success | Function | No| Called when API call is successful.|
  | fail | Function | No| Called when API call has failed.|
  | complete | Function | No| Called when API call is complete.|
Z
zengyawen 已提交
176

E
esterzhou 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
**success parameter**
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | data | [DownloadResponse](#downloadresponse) | Yes| Information returned when the download task is successful.|

**fail parameters**
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | data | any | Yes| Header information returned when the download task fails.|
  | code | number | Yes| HTTP status code returned when the download task fails.|

## DownloadResponse

**System capability**: SystemCapability.MiscServices.Download

  | Name| Type| Description|
  | -------- | -------- | -------- |
  | token | string | Download token, which is used to obtain the download status|


## request.onDownloadComplete
Z
zengyawen 已提交
198

E
esterzhou 已提交
199
onDownloadComplete(options: OnDownloadCompleteOptions): void
Z
zengyawen 已提交
200

E
esterzhou 已提交
201
Listens for download task status. This API returns no value.
Z
zengyawen 已提交
202

E
esterzhou 已提交
203
**System capability**: SystemCapability.MiscServices.Download
Z
zengyawen 已提交
204

E
esterzhou 已提交
205
**Parameters**
Z
zengyawen 已提交
206

E
esterzhou 已提交
207 208 209
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | options | [OnDownloadCompleteOptions](#ondownloadcompleteoptions) | Yes| Configurations of the download task.|
Z
zengyawen 已提交
210 211 212

**Example**

E
esterzhou 已提交
213 214 215 216 217 218 219 220 221 222 223 224
  ```js
  let onDownloadCompleteOptions = {
    token: 'token-index',
    success: function(data) {
      console.info(' download success, code:' + JSON.stringify(data));
    },
    fail: function(data, code) {
      console.info(' download fail data: ' + data + 'code: ' + code);
    },
    complete: function (){
      console.info(' download complete');
    }
Z
zengyawen 已提交
225
  }
E
esterzhou 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
  request.onDownloadComplete(onDownloadCompleteOptions);
  ```


## OnDownloadCompleteOptions

**System capability**: SystemCapability.MiscServices.Download

  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | token | string | Yes| Result token returned by the download API.|
  | success | Function | No| Called when API call is successful.|
  | fail | Function | No| Called when API call has failed.|
  | complete | Function | No| Called when API call is complete.|

**success parameter**
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | data | [OnDownloadCompleteResponse](#ondownloadcompleteresponse) | Yes| Information returned when the download task is successful.|

**fail parameters**
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | data | any | Yes| Header information returned when the download task fails.|
  | code | number | Yes| HTTP status code returned when the download task fails.|


## OnDownloadCompleteResponse

**System capability**: SystemCapability.MiscServices.Download

  | Name| Type| Description|
  | -------- | -------- | -------- |
  | uri | string | URI of the download file.|