• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @system.fetch (Data Request)
2
3> **NOTE**
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<sup>3+</sup>
18
19fetch(Object): void
20
21Obtains data through a network.
22
23**System capability**: SystemCapability.Communication.NetStack
24
25**Parameters**
26| Name| Type| Mandatory| Description|
27| -------- | -------- | -------- | -------- |
28| url | string | Yes| Resource URL.|
29| 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**.|
30| header | Object | No| Request header.|
31| method | string | No| Request method. The default value is **GET**. The value can be **OPTIONS**, **GET**, **HEAD**, **POST**, **PUT**, **DELETE **or **TRACE**.|
32| 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.|
33| success | Function | No| Called when the API call is successful. The return value is defined by [FetchResponse](#fetchresponse).|
34| fail | Function | No| Called when API call has failed.|
35| complete | Function | No| Called when the API call is complete.|
36
37**Table 1** Mapping between data and Content-Type
38
39| data | Content-Type | Description|
40| -------- | -------- | -------- |
41| string | Not set| The default value of Content-Type is **text/plain**, and the value of data is used as the request body.|
42| string | Any type| The value of data is used as the request body.|
43| 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.|
44| Object | application/x-www-form-urlencoded | The value of data is encoded based on the URL rule and is used as the request body.|
45
46## FetchResponse
47
48| Name| Type| Readable| Writable| Description|
49| -------- | -------- | -------- | -------- | -------- |
50| code | number | Yes| No| Server status code.|
51| data | string \| Object | Yes| No| The type of the returned data is determined by **responseType**. For details, see the mapping between **responseType** and **data** in **success** callback.|
52| headers | Object | Yes| No| All headers in the response from the server.|
53
54**Table 2** Mapping between responseType and data in success callback
55
56| responseType | data | Description|
57| -------- | -------- | -------- |
58| 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.|
59| text | string | Text content.|
60| json | Object | A JSON object.|
61
62**Example**
63
64```
65export default {
66  data: {
67    responseData: 'NA',
68    url: "test_url",
69  },
70  fetch: function () {
71    var that = this;
72    fetch.fetch({
73      url: that.url,
74      success: function(response) {
75        console.info("fetch success");
76        that.responseData = JSON.stringify(response);
77      },
78      fail: function() {
79        console.info("fetch fail");
80      }
81    });
82  }
83}
84```
85
86
87> **NOTE**
88>   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:
89>
90> ```
91> {
92>   "deviceConfig": {
93>     "default": {
94>       "network": {
95>         "cleartextTraffic": true
96>       }
97>       ...
98>     }
99>   }
100>   ...
101> }
102> ```
103