tencent cloud

Feedback

Initiate request

Last updated: 2024-07-12 19:31:16

    request

    This API is used via RequestTask wx.request(Object object).
    Feature Description: Initiates an HTTPS network request. Read relevant instructions before using this feature.
    Parameter and Description: Object.
    Attribute
    Type
    Default value
    Required
    Description
    url
    string
    -
    Yes
    Developer Server Interface Address
    data
    string/object/ArrayBuffer
    -
    No
    Parameters of the Request
    header
    Object
    -
    No
    Sets the request's header, but Referer is not allowed to be set in the header. The default content-type is application/json.
    method
    string
    GET
    No
    HTTP request method
    dataType
    string
    json
    No
    Format of returned data
    responseType
    string
    text
    No
    Response Data Type
    success
    function
    -
    No
    Callback Function of Successful Interface Call
    fail
    function
    -
    No
    Callback Function of Failing Interface Call
    complete
    function
    -
    No
    Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations)
    Valid values of object.method
    Value
    Description
    OPTIONS
    HTTP Request OPTIONS
    GET
    HTTP Request GET
    HEAD
    HTTP Request HEAD
    POST
    HTTP Request POST
    PUT
    HTTP Request PUT
    DELETE
    HTTP Request DELETE
    TRACE
    HTTP Request TRACE
    CONNECT
    HTTP Request CONNECT
    Valid values of object.dataType
    Value
    Description
    json
    The returned data is in JSON format. upon receipt, the data will undergo a JSON.parse operation.
    Other
    The data will not undergo a JSON.parse operation.
    Valid values of object.responseType
    Value
    Description
    text
    Response data is text.
    arraybuffer
    The response data is in ArrayBuffer format.
    object.success callback function:
    Parameter: Object res
    Attribute
    Type
    Description
    data
    string/Object/Arraybuffer
    Data returned from the developer's server.
    statusCode
    number
    HTTP status code returned from the developer's server.
    header
    Object
    HTTP Response Header returned from the developer's server.
    cookies
    Array.<string>
    Cookies returned by the developer server, formatted as an array of strings
    object.fail callback function
    Parameters: Object err
    Attribute
    Type
    Description
    errMsg
    String
    error message
    Return Value: RequestTask, the request task object.
    Data parameter description
    The final data sent to the server is of String type. If the data passed in is not of String type, it will be converted to String. The conversion rules are as follows:
    For data pertaining to the GET method, the data will be converted into a query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...).
    For data associated with the POST method and where header['content-type'] is application/json, the data will undergo JSON serialization.
    For data associated with the POST method and where header['content-type'] is application/x-www-form-urlencoded, the data will be converted into a query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...).
    Sample Code
    wx.request({
    url: 'test.php', // This is merely an example and not an actual interface address.
    data: {
    x: '',
    y: ''
    },
    header: {
    'content-type': 'application/json' // Default value
    },
    success(res) {
    console.log(res.data)
    }
    })

    RequestTask

    .abort

    This method is used via RequestTask.abort().
    Feature Description: Aborts the request task.

    .onChunkReceived

    This method is used via RequestTask.onChunkReceived(function listener).
    Feature Description: Monitors the Transfer-Encoding Chunk Received event. It is triggered when a new chunk is received.
    Parameter and Description: function listener, the listener function for the Transfer-Encoding Chunk Received event, with the following arguments Object res:
    Attribute
    Type
    Description
    res
    Object
    The Response every time when a new chunk is returned from the developer's server.
    Structure attribute of res
    Structure attributes
    Type
    Description
    data
    ArrayBuffer
    Returned chunk buffer

    .offChunkReceived

    This method is used via RequestTask.offChunkReceived(function listener).
    Feature Description: Removes the listener function for Transfer-Encoding Chunk Received events.
    Parameter and Description: function listener, the listener function passed in by onChunkReceived. If this parameter is not passed in, all listener functions will be removed.
    Sample Code
    const listener = function (res) { console.log(res) }
    
    RequestTask.onChunkReceived(listener)
    RequestTask.offChunkReceived(listener) // The same function object as the listener must be passed in.

    .onHeadersReceived

    This method is used via RequestTask.onHeadersReceived(function listener).
    Feature Description: Listens for HTTP Response Header events. It will precede the request completion event.
    Parameter and Description: function listener, HTTP Response Header event listener function.
    Attributes
    Type
    Description
    header
    Object
    HTTP Response Header returned by the developer server
    statusCode
    Number
    HTTP status code returned by the developer server (currently the statusCode field is not returned by the developer tools, you can view it on the real machine, it will be supported in the future)
    cookies
    Array.<string>
    Cookies returned by the developer server, formatted as an array of strings

    .offHeadersReceived

    This method is used via RequestTask.offHeadersReceived(function listener).
    Feature Description: Removes the listener function for the HTTP Response Header event.
    Parameter and Description: function listener, onHeadersReceived The listener function passed in, if this parameter is not passed, then all listener functions will be removed.
    Sample Code
    const listener = function (res) { console.log(res) }
    
    RequestTask.onHeadersReceived(listener)
    RequestTask.offHeadersReceived(listener) // need to pass in the same function object as the listener
    
    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