• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Data Request
2
3> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>
4> - The APIs of this module are no longer maintained since API version 6. You are advised to use [`@ohos.net.http`](js-apis-http.md) instead.
5>
6> - 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.
7
8
9## Modules to Import
10
11
12```
13import fetch from '@system.fetch';
14```
15
16
17## fetch.fetch
18
19fetch(Object): void
20
21Obtains data through a network.
22
23**Required permission**: ohos.permission.INTERNET
24
25**System capability**: SystemCapability.Communication.NetStack
26
27**Parameters**
28| Name| Type| Mandatory| Description|
29| -------- | -------- | -------- | -------- |
30| url | string | Yes| Resource URL.|
31| data | string \| Object | No| Request parameter, which can be a string or a JSON object. For details, see the mapping between **data** and **Content-Type**.|
32| header | Object | No| Request header.|
33| method | string | No| Request method. The default value is **GET**. The value can be **OPTIONS**, **GET**, **HEAD**, **POST**, **PUT**, **DELETE **or **TRACE**.|
34| responseType | string | No| Response type. The return type can be text or JSON. By default, the return type is determined based on **Content-Type** in the header returned by the server. For details, see return values in the **success** callback.|
35| success | Function | No| Called when the data is obtained successfully.|
36| fail | Function | No| Called when the data failed to be obtained.|
37| complete | Function | No| Called when the execution is complete.|
38
39**Table 1** Mapping between data and Content-Type
40
41| data | Content-Type | Description|
42| -------- | -------- | -------- |
43| string | Not set| The default value of Content-Type is **text/plain**, and the value of data is used as the request body.|
44| string | Any type| The value of data is used as the request body.|
45| Object | Not set| The default value of **Content-Type** is **application/x-www-form-urlencoded**. The **data** value is encoded based on the URL rule and appended in the request body.|
46| Object | application/x-www-form-urlencoded | The value of data is encoded based on the URL rule and is used as the request body.|
47
48Return values in the **success** callback
49
50| Name| Type| Description|
51| -------- | -------- | -------- |
52| code | number | Server status code.|
53| data | string \| Object | The type of the returned data is determined by **responseType**. For details, see the mapping between **responseType** and **data** in **success** callback.|
54| headers | Object | All headers in the response from the server.|
55
56**Table 2** Mapping between responseType and data in success callback
57
58| responseType | data | Description|
59| -------- | -------- | -------- |
60| N/A| string | When the type in the header returned by the server is **text/\***, **application/json**, **application/javascript**, or **application/xml**, the value is the text content.|
61| text | string | Text content.|
62| json | Object | A JSON object.|
63
64**Example**
65
66```
67export default {
68  data: {
69    responseData: 'NA',
70    url: "test_url",
71  },
72  fetch: function () {
73    var that = this;
74    fetch.fetch({
75      url: that.url,
76      success: function(response) {
77        console.info("fetch success");
78        that.responseData = JSON.stringify(response);
79      },
80      fail: function() {
81        console.info("fetch fail");
82      }
83    });
84  }
85}
86```
87
88
89> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>
90>   HTTPS is supported by default. To support HTTP, you need to add **"network"** to the **config.json** file, and set the attribute **"cleartextTraffic"** to **true**. That is:
91>
92> ```
93> {
94>   "deviceConfig": {
95>     "default": {
96>       "network": {
97>         "cleartextTraffic": true
98>       }
99>       ...
100>     }
101>   }
102>   ...
103> }
104> ```
105