目录

Request

has.request

has.request(Object object): RequestTask

Initiates an HTTPS network request.

Since version 1.0.4, a RequestTask object is returned when this API is called.

  • The API address of the developer server must use the HTTPS protocol and the server domain name must be configured in AppGallery Connect.

  • In versions earlier than ASCF 2.0.1, the TLS 1.2 encryption protocol is not supported. In ASCF 2.0.1 and later versions, the TLS 1.2 encryption protocol is supported.

Since: 1.0.0

Required permission: ohos.permission.INTERNET, which needs to be declared in module.json5

Precautions: Before calling this API, you need to configure the server domain name.

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
url string - Yes API URL of your server.
data string|Object|ArrayBuffer - No Request parameter.
header Object - No

Request header, which can be customized. The header cannot contain a referer.

The default value of Content-Type is application/json.

If there are multiple content-type fields, the value of first content-type is used to parse data when the ASCF version is earlier than 2.0.1. In 2.0.1 and later versions, the value of last content-type is used to parse data.

timeout number - No Timeout interval, in milliseconds. Default value: 60000
method string GET No HTTP request method.
dataType string json No Returned data type.
responseType string text No Response data type.
enableHttp2 boolean false No Whether to enable HTTP2.
enableProfile boolean true No Whether to enable the profile function. This function is enabled by default. When the function is enabled, you can view the performance debugging information from res.profile in the API callback.
enableCache boolean false No Whether to enable the HTTP cache.
enableChunked boolean false No

Whether to enable the transfer-encoding chunked function.

Since: 2.0.2

success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Valid values of method

Value Description
OPTIONS OPTIONS method.
GET GET method.
HEAD HEAD method.
POST POST method.
PUT PUT method.
DELETE DELETE method.
TRACE TRACE method.
CONNECT CONNECT method.

Valid values of responseType

Value Description
text Text.
arraybuffer ArrayBuffer.

Return value of success

Parameter Type Description
data string/object/arraybuffer Data returned by the server.
statusCode number HTTP status code returned by the server.
header object HTTP response header returned by the server.
cookies Array<string> Cookies returned by the server, in the format of string array.
profile Object Debugging information during network requests.

Returns

Network request object RequestTask.

Error code information

Error Code Error Message
102

Parameter error. Possible causes:

1.method is invalid.

2.url format is invalid.

201 Permission denied.
401

Parameter error. Possible causes:

1. Missing mandatory parameters.

2. Incorrect parameter type.

3. Parameter verification failed.

40000101

request error.

For details, see HTTP Error Codes.

Example:

has.request({
  url: 'https://www.example.com', // Replace the example with the actual API address.
  data: {
    x: '',
    y: ''
  },
  header: {
    'content-type': 'application/json'
  },
  success: (res) => {
    console.info(res.data);
  }
});

RequestTask

Defines a RequestTask object.

Since: 1.0.4

[h2]RequestTask.abort

RequestTask.abort()

Aborts a request task.

Since: 1.0.4

Example:

const requestTask = has.request({
  url: 'https://www.example.com', // Replace the example with the actual API address.
  data: {
    x: '',
    y: ''
  },
  header: {
    'content-type': 'application/json'
  },
  success: (res) => {
    console.info(res.data);
  }
})
requestTask.abort(); // Abort a network request.

[h2]RequestTask.onHeadersReceived

RequestTask.onHeadersReceived(function callback)

Enables listening for HTTP Response Header events, which occur earlier than request completion events.

Since: 1.0.4

Parameters

Parameter Type Mandatory Description
callback function Yes Callback for the HTTP Response Header event.

Return value of callback

Parameter Type Description
header object HTTP response header returned by the server.

Error code information

Error Code Error Message
102 Parameter error. Possible causes: listener should be a function.

Example:

const requestTask = has.request({
  url: 'https://www.example.com', // Replace the example with the actual API address.
  data: {
    x: '',
    y: ''
  },
  header: {
    'content-type': 'application/json'
  },
  success: (res) => {
    console.info(res.data);
  }
});
const callback = (res) => {
  console.info('Request onHeadersReceived', res);
};
requestTask.onHeadersReceived(callback);

[h2]RequestTask.offHeadersReceived

RequestTask.offHeadersReceived(function callback)

Disables listening for HTTP Response Header events.

Since: 1.0.4

Parameters

Parameter Type Mandatory Description
callback function No Callback passed in onHeadersReceived. If this parameter is left empty, listening will be disabled for all callbacks.

Error code information

Error Code Error Message
102 Parameter error. Possible causes: listener should be a function.

Example:

const requestTask = has.request({
  url: 'https://www.example.com', // Replace the example with the actual API address.
  data: {
    x: '',
    y: ''
  },
  header: {
    'content-type': 'application/json'
  },
  success: (res) => {
    console.info(res.data);
  }
});
const callback = (res) => {
  console.info('Request onHeadersReceived', res.header);
};
requestTask.onHeadersReceived(callback);
requestTask.offHeadersReceived(callback);

[h2]RequestTask.onChunkReceived

RequestTask.onChunkReceived(function callback)

Listens for the Transfer-Encoding Chunk Received event, which is triggered when a new chunk is received.

Since: 2.0.2

Parameters

Parameter Type Mandatory Description
callback function Yes Listener function for the Transfer-Encoding Chunk Received event.

Return value of callback

Parameter Type Description
data ArrayBuffer Returned chunk buffer.

Error code information

Error Code Error Message
102 Parameter error. Possible causes: listener should be a function.

Example:

const requestTask = has.request({
  url: 'https://www.example.com', // Replace the example with the actual API address.
  enableChunked: true,
  success: (res) => {
    console.info('request success', res);
  },
  fail: (err) => {
    console.error('request fail', err);
  }
});
const onChunkReceivedFn = (res) => {
  console.info('onChunkReceived callback triggered', res.data.byteLength);
};
requestTask.onChunkReceived(onChunkReceivedFn);

[h2]RequestTask.offChunkReceived

RequestTask.offChunkReceived(function callback)

Removes the listener function for the Transfer-Encoding Chunk Received event.

Since: 2.0.2

Parameters

Parameter Type Mandatory Description
callback function No Listener function passed by RequestTask.onChunkReceived. If this parameter is not passed, all listener functions are removed.
Error Code Error Message
102 Parameter error. Possible causes: listener should be a function.

Example:

const requestTask = has.request({
  url: 'https://www.example.com', // Replace the example with the actual API address.
  enableChunked: true,
  success: (res) => {
    console.info('request success', res);
  },
  fail: (err) => {
    console.error('request fail', err);
  }
});
const onChunkReceivedFn = (res) => {
  console.info('onChunkReceived callback triggered', res.data.byteLength);
};
requestTask.onChunkReceived(onChunkReceivedFn);
requestTask.offChunkReceived(onChunkReceivedFn);