We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
quickApp ReferencesQuick AppAPINetwork AccessFetch

Fetch

NOTICE

Data processed by the quick app APIs may be read by other apps when being used on a PC. Therefore, you are not advised to store sensitive data.

API Declaration

Add the following configuration to the features attribute in the manifest.json file:

Collapse
Word wrap
Dark theme
Copy code
  1. {"name": "system.fetch"}

Module Import

Add the following configuration to the script element on the page where the API will be called:

Collapse
Word wrap
Dark theme
Copy code
  1. import fetch from '@system.fetch'

Or

Collapse
Word wrap
Dark theme
Copy code
  1. var fetch = require("@system.fetch")

Application Scope

Expand

Item

Description

Applicable devices

Mobile phones, tablets, Vision products, and telematics devices

Applicable regions

Global

Definition

Expand

API

Description

fetch.fetch(OBJECT)

Fetches network data.

fetch.fetch(OBJECT)

Description

Fetches network data.

In 1060 and later versions, you can use config.network in the manifest.json file to configure the network timeout.

OBJECT parameters

Expand

Parameter

Type

Mandatory

Description

url

string

Yes

Resource URL.

data

string/object/arraybuffer

No

Request parameter, which can be a character string, JavaScript object, or ArrayBuffer object. For details, please refer to Relationship between data and Content-Type.

NOTE

If the data parameter is left empty, it is not passed.

header

object

No

Request header, which accommodates all attributes of the request.

Huawei proprietary specification (1073+): The Quick App Engine forcibly adds Referer to the header. Therefore, the referer information you configured before will be overwritten.

  • Referer added to the Quick App Center: https://quickapp.cn/[Quick app package name]/[Quick app version number]/page-frame.html
  • Referer added to Quick App Loader: https://quickapp.cn/dev/[Quick app package name]/[Quick app version number]/page-frame.html

Example: {"Accept-Encoding": "gzip, deflate","Accept-Language": "zh-CN,en-US;q=0.8,en;q=0.6","Referer":"https://quickapp.cn/com.abc.quickapp/1000/page-frame.html"}

method

string

No

The options are OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, and PATCH (1083+). The default value is GET.

responseType (1030+)

string

No

The options are text, json, file, and arraybuffer. By default, the return type is determined based on Content-Type in the header returned by the server. For details, please refer to Parameters returned in success.

success

function

No

Callback upon success.

fail

function

No

Callback upon failure. It may be caused by permission problems.

complete

function

No

Callback upon completion, regardless of whether the API call is successful or fails.

Relationship between data and Content-Type

Expand

data

Content-Type

Description

String

Not set

The default value of Content-Type is text/plain, and the value of data is used as the request body.

String

Any type

The value of data is used as the request body.

Object

Not set

The default value of Content-Type is application/x-www-form-urlencoded. The value of data is combined as the request body based on the URL rule.

Object

application/x-www-form-urlencoded

The value of data is encoded based on the URL rule and is used as the request body.

Object

Any type other than application/x-www-form-urlencoded

If the version is earlier than 1010, the call fails. From version 1010, if the declared minPlatformVersion is greater than or equal to 1010 in the manifest file, the value of data is converted into a character string as the request body.

ArrayBuffer (1030+)

Not set

The default value of Content-Type is application/octet-stream, and the value of data is used as the request body.

ArrayBuffer (1030+)

Any type

The value of data is used as the request body.

Parameters returned by success

Expand

Parameter

Type

Description

code

number

Server status code.

data

string/object/arraybuffer/json

For details, please refer to Relationship between responseType and data in success.

headers

object

All headers in the response from the server.

Relationship between responseType and data in success

Expand

responseType

data

Description

N/A

string

If the value of type in the header returned by the server is text/*, application/json, application/javascript, or application/xml, the value is text. Otherwise, the value is the URI of a temporary file. If the value is an image or a video, it can be displayed on the image or video control.

text

string

The common text is returned.

json

object

A JavaScript object is returned.

file

string

The URI of a stored temporary file is returned.

arraybuffer

arraybuffer

An ArrayBuffer object is returned.

Sample code

Collapse
Word wrap
Dark theme
Copy code
  1. fetch.fetch({
  2. url:"https://www.example.com",
  3. success:function(data){
  4. console.log("title: " + JSON.parse(data.data).title);
  5. },
  6. fail: function(data, code) {
  7. console.log("handling fail, code=" + code);
  8. }
  9. })

Security requirements

For HTTPS requests, the following certificate requirements should be observed.

  • The HTTPS certificate must be valid. The certificate must be trusted by the system and does not support self-signed certificates. The domain name of the website where the SSL certificate is deployed must be the same as the domain name for which the certificate is issued. The certificate must be within the validity period.
  • TLS 1.1 and later versions must be supported. Ensure that the HTTPS server supports TLS 1.1 and earlier versions. For security purposes, it is recommended that TLS 1.2 be supported. The following insecure algorithms are excluded in encryption suite configurations: DES, 3DES (except the scenario K1≠K2≠K3), SKIPJACK, RC2, RC4, MD2, MD4, SHA-1, MD5, and ANON. Some CAs may not be trusted by the operating system. Therefore, please check the operating system bulletins when selecting certificates.

Demo

Collapse
Word wrap
Dark theme
Copy code
  1. <template>
  2. <div>
  3. <div>
  4. <text>{{componentName}}</text>
  5. </div>
  6. <div>
  7. <div>
  8. <text>{{fetchData}}</text>
  9. <image show="{{showVar}}" src="{{photoUri}}"></image>
  10. </div>
  11. <input type="button" onclick="refresh" value="Update the GET instance." />
  12. </div>
  13. <div>
  14. <div>
  15. <text>{{fetchData1}}</text>
  16. </div>
  17. <input type="button" onclick="refresh1" value="Update the arraybuffer POST instance." />
  18. <input type="button" onclick="refresh2" value="Update the json POST instance." />
  19. <input type="button" onclick="refresh3" value="Update the text POST instance." />
  20. </div>
  21. </div>
  22. </template>
  23. <style>
  24. @import '../Common/css/common.css';
  25. .image {
  26. margin: 50px 0px;
  27. width: 600px;
  28. height: 140px;
  29. }
  30. .item-container {
  31. margin-bottom: 50px;
  32. margin-right: 60px;
  33. margin-left: 60px;
  34. flex-direction: column;
  35. }
  36. .item-content {
  37. flex-direction: column;
  38. background-color: #ffffff;
  39. padding: 30px;
  40. margin-bottom: 50px;
  41. align-items: center;
  42. justify-content: center;
  43. }
  44. .txt {
  45. line-height: 45px;
  46. padding-top: 15px;
  47. padding-bottom: 15px;
  48. }
  49. </style>
  50. <script>
  51. import fetch from '@system.fetch'
  52. import prompt from '@system.prompt'
  53. export default {
  54. data: {
  55. componentName: 'fetch',
  56. fetchData: '',
  57. fetchData1: '',
  58. photoUri: '',
  59. showVar: true,
  60. },
  61. onInit: function () {
  62. this.$page.setTitleBar({ text: 'fetch' })
  63. // this.getphoto()
  64. },
  65. postarraybuffer:function(){
  66. var that = this;
  67. var buffer = new ArrayBuffer(4);
  68. var a = new Uint16Array(buffer);
  69. a[0] = 1;
  70. a[1] = 2;
  71. fetch.fetch({
  72. url: "https://httpbin.org/post",
  73. data:buffer,
  74. responseType:'arraybuffer',
  75. method:"POST",
  76. success: function (ret) {
  77. var buf1 = ret.data;
  78. that.fetchData1 = "Obtain data: " + JSON.stringify(String.fromCharCode.apply(null, new Uint8Array(buf1)));
  79. that.showVar = true;
  80. prompt.showToast({
  81. message: "success"
  82. })
  83. },
  84. fail: function (msg, code) {
  85. console.log(msg, code);
  86. },
  87. complete: function () {
  88. console.log("complete");
  89. }
  90. })
  91. },
  92. postjson:function(){
  93. var that = this;
  94. var str = '{"username":"123","password":"123"}';
  95. fetch.fetch({
  96. url: "https://httpbin.org/post",
  97. data:JSON.parse(str),
  98. header:{"User-Agent": "Mozilla/5.0 (Linux; U; Android 7.0; zh-cn; STF-AL00 Build/HUAWEISTF-AL00) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 Chrome/37.0.0.0 MQQBrowser/7.9 Mobile Safari/537.36","Accept-Encoding": "gzip, deflate","Accept-Language": "zh-CN,en-US;q=0.8,en;q=0.6"},
  99. responseType:'json',
  100. method:"POST",
  101. success: function (ret) {
  102. that.fetchData1 = "Obtain data: " + JSON.stringify(ret.data);
  103. that.showVar = true;
  104. prompt.showToast({
  105. message: "success"
  106. })
  107. },
  108. fail: function (msg, code) {
  109. console.log(msg, code);
  110. },
  111. complete: function () {
  112. console.log("complete");
  113. }
  114. })
  115. },
  116. poststring:function(){
  117. var that = this;
  118. var str = '"name"="huaweitest"&"password":"123"';
  119. fetch.fetch({
  120. url: "https://httpbin.org/post",
  121. responseType:'text',
  122. data:str,
  123. method:"POST",
  124. success: function (ret) {
  125. that.fetchData1 = "Obtain data: " + JSON.stringify(ret.data);
  126. that.showVar = true;
  127. prompt.showToast({
  128. message: "success"
  129. })
  130. },
  131. fail: function (msg, code) {
  132. console.log(msg, code);
  133. },
  134. complete: function () {
  135. console.log("complete");
  136. }
  137. })
  138. },
  139. getphoto: function () {
  140. var that = this;
  141. fetch.fetch({
  142. url: "https://www.huawei.com/Assets/CBG/img/logo.png",
  143. success: function (ret) {
  144. that.fetchData = "Obtain data: " + JSON.stringify(ret.data);// Note that the data type of that.fetchData must be the same as that of ret.
  145. that.photoUri = ret.data;
  146. that.showVar = true;
  147. prompt.showToast({
  148. message: "success"
  149. })
  150. },
  151. fail: function (msg, code) {
  152. console.log(msg, code);
  153. },
  154. complete: function () {
  155. console.log("complete");
  156. }
  157. })
  158. },
  159. refresh: function () {
  160. this.getphoto();
  161. },
  162. refresh1: function () {
  163. this.postarraybuffer();
  164. },
  165. refresh2: function () {
  166. this.postjson();
  167. },
  168. refresh3: function () {
  169. this.poststring();
  170. }
  171. }
  172. </script>

Version Change History

Expand

Version

Date

Description

1083

2022-03-21

Added PATCH for method of the fetch API.

Links

Search in References
Enter a keyword.