目录

Request Task

API Declaration

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

{"name": "system.requesttask"}

Module Import

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

import requesttask from '@system.requesttask'

Or

var requesttask = require("@system.requesttask")

Application Scope

Item

Description

Applicable devices

Mobile phones and tablets

Applicable regions

Global

Definition

API

Description

requesttask.request(OBJECT)

Creates a network request. Each time this API is successfully called, a new RequestTask object is returned.

requestTask.onProgressUpdate(function callback)

Listens to request progress change events.

requestTask.offProgressUpdate(function callback)

Cancels the listening of request progress change events.

requestTask.onHeadersReceived(function callback)

Listens to HTTP response header events.

requestTask.offHeadersReceived(OBJECT)

Cancels listening to HTTP response header events.

requestTask.abort()

Suspends a request task.

[h2]requesttask.request(OBJECT) (1078+, Not Formulated by the Vendor Alliance)

Description

Creates a network request. Each time this API is successfully called, a new RequestTask object is returned.

Parameters

Parameter

Type

Mandatory/Optional

Description

url

string

Mandatory

Resource URL.

data

string/object/arraybuffer

Optional

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

header

object

Optional

Request header, which accommodates all attributes of the request. Since version 1073, Huawei 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 Engine: 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

Optional

The options are OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, and CONNECT. The default value is GET.

responseType

string

Optional

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.

timeout

number

Optional

Connection timeout interval, but not the read/write timeout interval. The unit is millisecond and the default value is 10000.

success

function

Optional

Callback upon success.

fail

function

Optional

Callback upon failure.

complete

function

Optional

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

The value of data is converted into a character string as the request body.

ArrayBuffer

Not set

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

ArrayBuffer

Any type

The value of data is used as the request body.

Parameters returned in success

Parameter

Type

Description

statusCode

number

Server status code.

statusText

string

Server status code.

data

string/object/arraybuffer/json

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

header

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

this.request_task = requesttask.request({
    url: 'https://httpbin.org/anything',
    method:'POST',
    data:"12313213213212",
    success(res) {
        console.log("Request success. resp = " + JSON.stringify(res))
    },
    fail(res) {
        console.log("Request fail. resp = " + JSON.stringify(res))
    }
})

[h2]requestTask.onProgressUpdate(function callback) (1078+, Not Formulated by the Vendor Alliance)

Description

Listens to request progress change events.

This parameter is valid only for the POST, PUT, and CONNECT methods.

Parameters

Callback function of request progress change events.

Parameters returned in callback

Object type.

Parameter

Type

Description

progress

number

Request sending progress, in percentage.

totalBytesSent

number

Size of the sent data, in bytes.

totalBytesExpectedToSend

number

Anticipated size of all data to be sent, in bytes.

Sample code

this.request_task.onProgressUpdate(res => {
    console.log("Request progress update = " + JSON.stringify(res))
})

[h2]requestTask.offProgressUpdate(function callback) (1078+, Not Formulated by the Vendor Alliance)

Description

Cancels the listening of request progress change events.

Parameters

Callback function of request progress change events.

Sample code

this.request_task.offProgressUpdate()

[h2]requestTask.onHeadersReceived(function callback) (1078+, Not Formulated by the Vendor Alliance)

Description

Listens to HTTP response header events. This API is called before the request success callback.

Parameters

Callback function of HTTP response header events.

Parameters returned in callback

Object type.

Parameter

Type

Description

header

object

HTTP response header returned by your server.

Sample code

this.request_task.onHeadersReceived(res => {
    console.log("Request onHeaderReceived = " + JSON.stringify(res))
})

[h2]requestTask.offHeadersReceived(OBJECT) (1078+, Not Formulated by the Vendor Alliance)

Description

Cancels listening to HTTP response header events.

Parameters

Callback function of HTTP response header events.

Sample code

this.request_task.offHeadersReceived()

[h2]requestTask.abort() (1078+, Not Formulated by the Vendor Alliance)

Description

Suspends a request task.

Sample code

this.request_task.abort()