Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
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.
Add the following configuration to the features attribute in the manifest.json file:
- {"name": "system.fetch"}
Add the following configuration to the script element on the page where the API will be called:
- import fetch from '@system.fetch'
Or
- var fetch = require("@system.fetch")
Item | Description |
|---|---|
Applicable devices | Mobile phones, tablets, Vision products, and telematics devices |
Applicable regions | Global |
API | Description |
|---|---|
Fetches network data. |
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
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.
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
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
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
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
- fetch.fetch({
- url:"https://www.example.com",
- success:function(data){
- console.log("title: " + JSON.parse(data.data).title);
- },
- fail: function(data, code) {
- console.log("handling fail, code=" + code);
- }
- })
Security requirements
For HTTPS requests, the following certificate requirements should be observed.
- <template>
- <div>
- <div>
- <text>{{componentName}}</text>
- </div>
- <div>
- <div>
- <text>{{fetchData}}</text>
- <image show="{{showVar}}" src="{{photoUri}}"></image>
- </div>
- <input type="button" onclick="refresh" value="Update the GET instance." />
- </div>
- <div>
- <div>
- <text>{{fetchData1}}</text>
- </div>
- <input type="button" onclick="refresh1" value="Update the arraybuffer POST instance." />
- <input type="button" onclick="refresh2" value="Update the json POST instance." />
- <input type="button" onclick="refresh3" value="Update the text POST instance." />
- </div>
- </div>
- </template>
- <style>
- @import '../Common/css/common.css';
- .image {
- margin: 50px 0px;
- width: 600px;
- height: 140px;
- }
- .item-container {
- margin-bottom: 50px;
- margin-right: 60px;
- margin-left: 60px;
- flex-direction: column;
- }
- .item-content {
- flex-direction: column;
- background-color: #ffffff;
- padding: 30px;
- margin-bottom: 50px;
- align-items: center;
- justify-content: center;
- }
- .txt {
- line-height: 45px;
- padding-top: 15px;
- padding-bottom: 15px;
- }
- </style>
- <script>
- import fetch from '@system.fetch'
- import prompt from '@system.prompt'
- export default {
- data: {
- componentName: 'fetch',
- fetchData: '',
- fetchData1: '',
- photoUri: '',
- showVar: true,
- },
- onInit: function () {
- this.$page.setTitleBar({ text: 'fetch' })
- // this.getphoto()
- },
- postarraybuffer:function(){
- var that = this;
- var buffer = new ArrayBuffer(4);
- var a = new Uint16Array(buffer);
- a[0] = 1;
- a[1] = 2;
- fetch.fetch({
- url: "https://httpbin.org/post",
- data:buffer,
- responseType:'arraybuffer',
- method:"POST",
- success: function (ret) {
- var buf1 = ret.data;
- that.fetchData1 = "Obtain data: " + JSON.stringify(String.fromCharCode.apply(null, new Uint8Array(buf1)));
- that.showVar = true;
- prompt.showToast({
- message: "success"
- })
- },
- fail: function (msg, code) {
- console.log(msg, code);
- },
- complete: function () {
- console.log("complete");
- }
- })
- },
- postjson:function(){
- var that = this;
- var str = '{"username":"123","password":"123"}';
- fetch.fetch({
- url: "https://httpbin.org/post",
- data:JSON.parse(str),
- 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"},
- responseType:'json',
- method:"POST",
- success: function (ret) {
- that.fetchData1 = "Obtain data: " + JSON.stringify(ret.data);
- that.showVar = true;
- prompt.showToast({
- message: "success"
- })
- },
- fail: function (msg, code) {
- console.log(msg, code);
- },
- complete: function () {
- console.log("complete");
- }
- })
- },
- poststring:function(){
- var that = this;
- var str = '"name"="huaweitest"&"password":"123"';
- fetch.fetch({
- url: "https://httpbin.org/post",
- responseType:'text',
- data:str,
- method:"POST",
- success: function (ret) {
- that.fetchData1 = "Obtain data: " + JSON.stringify(ret.data);
- that.showVar = true;
- prompt.showToast({
- message: "success"
- })
- },
- fail: function (msg, code) {
- console.log(msg, code);
- },
- complete: function () {
- console.log("complete");
- }
- })
- },
- getphoto: function () {
- var that = this;
- fetch.fetch({
- url: "https://www.huawei.com/Assets/CBG/img/logo.png",
- success: function (ret) {
- that.fetchData = "Obtain data: " + JSON.stringify(ret.data);// Note that the data type of that.fetchData must be the same as that of ret.
- that.photoUri = ret.data;
- that.showVar = true;
- prompt.showToast({
- message: "success"
- })
- },
- fail: function (msg, code) {
- console.log(msg, code);
- },
- complete: function () {
- console.log("complete");
- }
- })
- },
- refresh: function () {
- this.getphoto();
- },
- refresh1: function () {
- this.postarraybuffer();
- },
- refresh2: function () {
- this.postjson();
- },
- refresh3: function () {
- this.poststring();
- }
- }
- </script>
Version | Date | Description |
|---|---|---|
1083 | 2022-03-21 | Added PATCH for method of the fetch API. |