Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat program API initiates the request


May 18, 2021 WeChat Mini Program Development Document


Table of contents


RequestTask wx.request(Object object)

Launch an HTTPS network request. Please read the instructions before use.

Parameters

Object object

Property Type The default Required Description The lowest version
Url string Is Developer server interface address
data string/object/ArrayBuffer Whether The requested parameter
header Object Whether The requested header cannot be set in the header.
content-type defaults application/json
timeout number Whether Time-out in milliseconds 2.10.0
method string GET Whether HTTP request method
dataType string Json Whether The data format returned
responseType string text Whether The data type of the response 1.7.0
enableHttp2 boolean false Whether Turn on http2 2.10.4
enableQuic boolean false Whether Turn on quic 2.10.4
enableCache boolean false Whether Turn on cache 2.10.4
success function Whether The interface calls a successful callback function
fail function Whether The interface calls the failed callback function
complete function Whether Callback function at end of interface call (call succeeds, fails are executed)

The legal value of object.method

Value Description The lowest version
OPTIONS HTTP request OPTIONS
GET HTTP requests GET
HEAD HTTP requests HEAD
POST HTTP requests POST
PUT HTTP request PUT
DELETE HTTP requests DELETE
TRACE HTTP request TRACE
CONNECT HTTP requests CONNECT

The legal value of object.dataType

Value Description The lowest version
Json The returned data is JSON, and once returned, JSON.parse is performed on the returned data
Other JSON.parse is not performed on the returned content

The legal value of object.responseType

Value Description The lowest version
text The data for the response is text
arraybuffer The data in response is ArrayBuffer

object.success callback function

Parameters
Object res
Property Type Description The lowest version
data string/Object/Arraybuffer The data returned by the developer server
statusCode number The HTTP status code returned by the developer server
header Object HTTP Response Header returned by the developer server 1.2.0
cookies Array.<string> The cookies returned by the developer server in the form of an array of strings 2.10.0
profile Object Some debug information during the network request process 2.10.4

The structure of res.profile

Property Type Description
redirectStart number The time at which the first HTTP redirect occurred. Redirects that have a jump and are within the same domain name are counted, otherwise the value is 0
redirectEnd number The time at which the last HTTP redirect was completed. A redirect that has a jump and is inside the same domain name is counted, otherwise the value is 0
fetchStart number The time the component is ready to crawl resources using HTTP requests, which occurs before the local cache is checked
domainLookupStart number The time at which DNS domain name queries start, if a local cache (i.e. no DNS queries) or persistent connections are used, are equal to the fetchStart value
domainLookupEnd number The time at which DNS domain name queries are completed is equal to the fetchStart value if a local cache (i.e., no DNS queries) or persistent connections are used
connectStart number Http (TCP) starts to establish a connection at the same time as fetchStart values if it is a persistent connection. Note If an error occurs at the transport layer and the connection is re-established, the time at which the newly established connection starts is shown here
connectEnd number Http (TCP) completes the time it takes to establish the connection (completes the handshake), or is equal to the fetchStart value if the connection is persistent. N ote If an error occurs in the transport layer and the connection is re-established, the time when the newly established connection is completed is shown here. Note that the handshake here ends, including secure connection establishment completed and SOCKS authorization passed
SSLconnectionStart number The time the SSL established the connection, or 0 if it is not a secure connection
SSLconnectionEnd number The time the SSL was established to complete, or 0 if it is not a secure connection
requestStart number HTTP requests to read the time at which the real document started (the connection was established), including reading the cache locally. When the connection is incorrectly reconnected, it is also shown here when the new connection was established
requestEnd number The time at which the HTTP request reads the end of the real document
responseStart number The time it took for HTTP to start receiving responses (getting to the first byte), including reading the cache locally
responseEnd number The HTTP response all receives the completed time (getting to the last byte), including reading the cache locally
Rtt number Rtt in real time during the connection of the request
estimate_nettype string Assessed network status slow 2g/2g/3g/4g
httpRttEstimate number The protocol layer evaluates the rtt of the current network based on multiple requests (for informational purposes only)
transportRttEstimate number Rtt for the current network evaluated by the transport layer based on multiple requests (for informational purposes only)
downstreamThroughputKbpsEstimate number Evaluate the kbps currently downloaded on the network
throughputKbps number The actual download of kbps for the current network
peerIP string The IP currently requested
port number The port currently requested
socketReused boolean Whether to re-use the connection
sendBytesCount number The number of bytes sent
receivedBytedCount number The number of bytes received

Returns a value

RequestTask

Base library 1.4.0 starts to be supported, and low versions need to be compatible.

Request a task object

Data parameter description

The data eventually sent to the server is the String type, which is converted to String if the incoming data is not a String type. The conversion rules are as follows:

  • For the get method's data, the data is converted to query string (encodeURIComponent (k) s/encodeURIComponent (v) and encodeURIComponent (k) s/encodeURIComponent (v)...
  • For the POST method and header(content-type') is applied/json's data, the data is serialized JSON
  • For the POST method and the header is applied/x-www-form-urlencoded data, the data is converted into query string (encodeURIComponent (k) s encodeURIComponent (v) and encodeURIComponent (k) s encodeURIComponent (v)...

The sample code

wx.request({
  url: 'test.php', //仅为示例,并非真实的接口地址
  data: {
    x: '',
    y: ''
  },
  header: {
    'content-type': 'application/json' // 默认值
  },
  success (res) {
    console.log(res.data)
  }
})

RequestTask

Base library 1.4.0 starts to be supported, and low versions need to be compatible.

The network requests the task object

Method

RequestTask.abort()

Base library 1.4.0 starts to be supported, and low versions need to be compatible.

Interrupt the request task


RequestTask.offHeadersReceived(function callback)

Base library 2.1.0 starts to support, and low versions need to be compatible.

Cancel listening to the HTTP Response Header event

Parameters

function callback

The callback function for the HTTP Response Header event


RequestTask.onHeadersReceived(function callback)

Base library 2.1.0 starts to support, and low versions need to be compatible.

Listen for http Response Header events. the event will be completed earlier than requested

Parameters

function callback

The callback function for the HTTP Response Header event

Parameters

Object res
Property Type Description
header Object HTTP Response Header returned by the developer server


The sample code

const requestTask = wx.request({
  url: 'test.php', //仅为示例,并非真实的接口地址
  data: {
    x: '' ,
    y: ''
  },
  header: {
    'content-type': 'application/json'
  },
  success (res) {
    console.log(res.data)
  }
})
requestTask.abort() // 取消请求任务