1e41f4b71Sopenharmony_ci# @system.fetch (Data Request)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci> **NOTE**
4e41f4b71Sopenharmony_ci> - 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).
5e41f4b71Sopenharmony_ci> 
6e41f4b71Sopenharmony_ci> - 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.
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## Modules to Import
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci```
13e41f4b71Sopenharmony_ciimport fetch from '@system.fetch';
14e41f4b71Sopenharmony_ci```
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci## fetch.fetch<sup>3+</sup>
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_cifetch(options:{ <br>
20e41f4b71Sopenharmony_ci&nbsp;&nbsp;url: string;<br>
21e41f4b71Sopenharmony_ci&nbsp;&nbsp;data?: string | object;<br>
22e41f4b71Sopenharmony_ci&nbsp;&nbsp;header?: Object;<br>
23e41f4b71Sopenharmony_ci&nbsp;&nbsp;method?: string;<br>
24e41f4b71Sopenharmony_ci&nbsp;&nbsp;responseType?: string;<br>
25e41f4b71Sopenharmony_ci&nbsp;&nbsp;success?: (data: FetchResponse) => void;<br>
26e41f4b71Sopenharmony_ci&nbsp;&nbsp;fail?: (data: any, code: number) => void;<br>
27e41f4b71Sopenharmony_ci&nbsp;&nbsp;complete?: () => void;<br>
28e41f4b71Sopenharmony_ci  } ): void
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ciObtains data through a network.
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci**Parameters**
35e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
36e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
37e41f4b71Sopenharmony_ci| url | string | Yes| Resource URL.|
38e41f4b71Sopenharmony_ci| 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**.|
39e41f4b71Sopenharmony_ci| header | Object | No| Request header.|
40e41f4b71Sopenharmony_ci| method | string | No| Request method. The default value is **GET**. The value can be **OPTIONS**, **GET**, **HEAD**, **POST**, **PUT**, **DELETE **or **TRACE**.|
41e41f4b71Sopenharmony_ci| 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.|
42e41f4b71Sopenharmony_ci| success | Function | No| Called when the API call is successful. The return value is defined by [FetchResponse](#fetchresponse3).|
43e41f4b71Sopenharmony_ci| fail | Function | No| Called when an API call fails.|
44e41f4b71Sopenharmony_ci| complete | Function | No| Called when an API call is complete.|
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci**Table 1** Mapping between data and Content-Type
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci| data | Content-Type | Description|
49e41f4b71Sopenharmony_ci| -------- | -------- | -------- |
50e41f4b71Sopenharmony_ci| string | Left unspecified| The default value of Content-Type is **text/plain**, and the value of data is used as the request body.|
51e41f4b71Sopenharmony_ci| string | Any type| The value of data is used as the request body.|
52e41f4b71Sopenharmony_ci| Object | Left unspecified| 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.|
53e41f4b71Sopenharmony_ci| Object | application/x-www-form-urlencoded | The value of data is encoded based on the URL rule and is used as the request body.|
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci## FetchResponse<sup>3+</sup>
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetStack
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci| Name| Type| Readable| Writable| Description|
60e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- |
61e41f4b71Sopenharmony_ci| code | number | Yes| No| Server status code.|
62e41f4b71Sopenharmony_ci| 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.|
63e41f4b71Sopenharmony_ci| headers | Object | Yes| No| All headers in the response from the server.|
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci**Table 2** Mapping between responseType and data in success callback
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci| responseType | data | Description|
68e41f4b71Sopenharmony_ci| -------- | -------- | -------- |
69e41f4b71Sopenharmony_ci| 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.|
70e41f4b71Sopenharmony_ci| text | string | Text content.|
71e41f4b71Sopenharmony_ci| json | Object | A JSON object.|
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci**Example**
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci```
76e41f4b71Sopenharmony_ciexport default {
77e41f4b71Sopenharmony_ci  data: {
78e41f4b71Sopenharmony_ci    responseData: 'NA',
79e41f4b71Sopenharmony_ci    url: "test_url",
80e41f4b71Sopenharmony_ci  },
81e41f4b71Sopenharmony_ci  fetch: function () {
82e41f4b71Sopenharmony_ci    var that = this;
83e41f4b71Sopenharmony_ci    fetch.fetch({
84e41f4b71Sopenharmony_ci      url: that.url,
85e41f4b71Sopenharmony_ci      success: function(response) {
86e41f4b71Sopenharmony_ci        console.info("fetch success");
87e41f4b71Sopenharmony_ci        that.responseData = JSON.stringify(response);
88e41f4b71Sopenharmony_ci      },
89e41f4b71Sopenharmony_ci      fail: function() {
90e41f4b71Sopenharmony_ci        console.info("fetch fail");
91e41f4b71Sopenharmony_ci      }
92e41f4b71Sopenharmony_ci    });
93e41f4b71Sopenharmony_ci  }
94e41f4b71Sopenharmony_ci}
95e41f4b71Sopenharmony_ci```
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci> **NOTE**
99e41f4b71Sopenharmony_ci>   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**, as shown below:
100e41f4b71Sopenharmony_ci>   
101e41f4b71Sopenharmony_ci> ```
102e41f4b71Sopenharmony_ci> {
103e41f4b71Sopenharmony_ci>   "deviceConfig": {
104e41f4b71Sopenharmony_ci>     "default": {
105e41f4b71Sopenharmony_ci>       "network": {
106e41f4b71Sopenharmony_ci>         "cleartextTraffic": true
107e41f4b71Sopenharmony_ci>       }
108e41f4b71Sopenharmony_ci>       ...
109e41f4b71Sopenharmony_ci>     }
110e41f4b71Sopenharmony_ci>   }
111e41f4b71Sopenharmony_ci>   ...
112e41f4b71Sopenharmony_ci> }
113e41f4b71Sopenharmony_ci> ```
114