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.
Configure server domain names in AppGallery Connect in advance. The server API URL must support HTTPS.
Since: 1.0.0
Required permission: ohos.permission.INTERNET, which needs to be declared in module.json5
Note: Before calling this API, you need to configure server domain names in AppGallery Connect.
Parameters
The input parameter is an object containing the following fields.
Name | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
url | string | - | Yes | API URL of your server. |
data | string/object/ArrayBuffer | - | No | Request parameter. |
header | Object | - | No | HTTP request header, which cannot contain Referer. The default value of Content-Type is application/json. |
timeout | number | - | No | Timeout duration, in ms. 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. 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. |
success | function | - | No | Callback to be invoked when the API call is successful. |
fail | function | - | No | Callback to be invoked when the API call fails. |
complete | function | - | No | Callback to be invoked when the API call is complete, regardless of success or failure. |
Return value of success
Name | 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. |
Return value
A RequestTask object.
Example
has.request({
url: 'https://www.example.com', // Replace the example URL with the actual URL.
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json'
},
success(res) {
console.log(res.data);
}
}); RequestTask.abort()
Destroys an HTTP request.
Since: 1.0.4
Example
const requestTask = has.request({
url: 'https://www.example.com', // Replace the example URL with the actual URL.
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json'
},
success(res) {
console.log(res.data);
}
})
requestTask.abort(); // Abort a network request. RequestTask.onHeadersReceived(function callback)
Enables listening for HTTP Response Header events, which occur earlier than request completion events.
Since: 1.0.4
Parameters
Name | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback for the HTTP Response Header event. |
Return value of callback
Name | Type | Description |
|---|---|---|
header | object | HTTP response header returned by the server. |
Example
const requestTask = has.request({
url: 'https://www.example.com', // Replace the example URL with the actual URL.
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json'
},
success(res) {
console.log(res.data);
}
});
const callback = function (res) {
console.log('Request onHeadersReceived: ' + JSON.stringify(res.header));
};
requestTask.onHeadersReceived(callback); RequestTask.offHeadersReceived(function callback)
Disables listening for HTTP Response Header events.
Since: 1.0.4
Parameters
Name | Type | Mandatory | Description |
|---|---|---|---|
callback | function | No | Callback passed in onHeadersReceived. If this parameter is left empty, listening will be disabled for all callbacks. |
Example
const requestTask = has.request({
url: 'https://www.example.com', // Replace the example URL with the actual URL.
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json'
},
success(res) {
console.log(res.data);
}
});
const callback = function (res) {
console.log('Request onHeadersReceived: ' + JSON.stringify(res.header));
};
requestTask.onHeadersReceived(callback);
requestTask.offHeadersReceived(callback);