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