tencent cloud

All product documents
Tencent Cloud Super App as a Service
Initiate request
Last updated: 2025-02-24 17:10:35
Initiate request
Last updated: 2025-02-24 17:10:35

request

This API is called using RequestTask  wx.request(Object object).
Feature description:Initiates an HTTPS network request. Before using this API, please carefully read Network.
Parameter and description:Object object.
Property
Type
Default value
‍Required
Description
url
string
-
True
The URL of the developer's server API
data
string/object/ArrayBuffer
-
False
Request parameter.
header
Object
-
False
Sets the request header. Referer cannot be set in the header. The default content-type is application/json
timeout
number
-
False
Timeout duration in milliseconds.
method
string
GET
False
HTTP request method.
dataType
string
json
False
The format of the returned data.
responseType
string
text
False
The type of the response data.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
Valid values for object.method:
Value
Description
OPTIONS
HTTP OPTIONS request
GET
HTTP GET request
HEAD
HTTP HEAD request
POST
HTTP POST request
PUT
HTTP PUT request
DELETE
HTTP DELETE request
TRACE
HTTP TRACE request
CONNECT
HTTP CONNECT request
Valid values for object.dataType:
Value
Description
json
The returned data is in JSON format and will be parsed once using JSON.parse.
Others
The returned data will not be parsed using JSON.parse
Valid values for object.responseType
Value
Description
text
The response data is text.
arraybuffer
The response data is an ArrayBuffer.
object.success callback function
Parameter: Object res.
Property
Type
Description
data
string/Object/Arraybuffer
The data returned by the developer server.
statusCode
number
The HTTP status code returned by the developer server.
header
Object
The HTTP response header returned by the developer server.
cookies
Array.<string>
The cookies returned by the developer server in the format of string array (not supported in mini games).
object.fail callback function
Parameter: Object err
Property
Type
Description
errMsg
String
Error message.
Return value:RequestTask . Request task object.
Description of the data parameter:
The data ultimately sent to the server is of type String. If the passed data is not of type String, it will be converted to String. The conversion rules are as follows:
For GET method data, it will be converted to a query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)
For POST method data with header['content-type'] as application/json, it will be serialized into JSON.
For POST method data with header['content-type'] as application/x-www-form-urlencoded , it will be converted into a query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)
Example:
wx.request({
url: 'test.php', // Example URL, not a real interface address
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json' // Default value
},
success(res) {
console.log(res.data)
}
})

RequestTask

.abort

This method is called using RequestTask.abort().
Feature description:Aborts a request task.

.onChunkReceived

This method is called using RequestTask.onChunkReceived(function listener).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Listens for the Transfer-Encoding Chunk Received event. It is triggered when a new chunk is received.
Parameter and description:function listener. The listener for the Transfer-Encoding Chunk Received event. Parameter: Object res.
Property
Type
Description
res
Object
The response for each new chunk received from the developer's server.
res structure property
Structural property
Type
Description
data
ArrayBuffer
The returned chunk buffer.

.offChunkReceived

This method is called using RequestTask.offChunkReceived(function listener).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Removes the listener for the Transfer-Encoding Chunk Received event.
Parameter and description:function listener. The listener passed to onChunkReceived. If this parameter is not provided, all listeners will be removed.
Example
const listener = function (res) { console.log(res) }

RequestTask.onChunkReceived(listener)
RequestTask.offChunkReceived(listener) // Must pass the same function object used in onChunkReceived

.onHeadersReceived

This method is called using RequestTask.onHeadersReceived(function listener).
Feature description:Listens for the HTTP Response Header event. The event is triggered before the response is received.
Parameter and description:function listener. The listener for the HTTP Response Header event.
Property
Type
Description
header
Object
The HTTP response header returned by the developer server.
statusCode
Number
The HTTP status code returned by the developer's server (currently not returned in the developer tools, but can be viewed on a real device)
cookies
Array.<string>
The cookies returned by the developer server in the format of string array.

.offHeadersReceived

This method is called using RequestTask.offHeadersReceived(function listener).
Feature description:Removes the listener for the HTTP Response Header event.
Parameter and description:function listener. The listener function passed to onHeadersReceived. If this parameter is not provided, all listeners will be removed.
Example:
const listener = function (res) { console.log(res) }

RequestTask.onHeadersReceived(listener)
RequestTask.offHeadersReceived(listener) // Must pass the same function object used in onHeadersReceived


Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon