Skip to content

Network

TIP

When using network-related APIs in mini programs, you need to pay attention to the following issues. Developers are advised to understand them in advance.

Server domain name configuration

Each mini program needs to set a communication domain name in advance. The mini program can only communicate with the specified domain name, including ordinary HTTPS requests (wx.request), file uploads (wx.uploadFile), file downloads (wx.downloadFile) and Websocket communications (wx.connectSocket), UDP communications (wx.createUDPSocket), and TCP communications (wx.createTCPSocket).

The network interface is only allowed to communicate with non-local IPs in the same LAN and configured server domain names.

Configuration process

Please configure the server domain name in the mini program background > mini program management > development management > domain name settings. When configuring, please note:

  • The domain name supports https ( wx.request wx.uploadFile wx.downloadFile ) protocol, wss ( wx.connectSocket ) protocol, tcp protocol and udp protocol;

  • The domain name cannot use IP addresses or localhost;

  • Ports cannot be configured, such as http://miniApp.com, but ports can be included in the requested URL, such as http://miniApp.com:8080.

  • For wss domain names, there is no need to configure ports. By default, all ports under the domain name are allowed to be requested.

  • Configuring the parent domain name is not supported. Use subdomains.

Network request

Timeout

  • The default timeout and maximum timeout are both 60s.
  • The timeout can be configured in app.json.
  • You can also specify a timeout when calling the interface, such as wx.request({ timeout: 5000 }), in milliseconds. The timeout configuration of the interface call takes precedence over the configuration in app.json.

Usage restrictions.

  • The referer header of the network request cannot be set. Its format is fixed as https://appservice.wx.com/{appid}/{version}/page-frame.html, where {appid} is the appid of the mini program, and {version} is the version number of the mini program. Version number 0 indicates the development version, trial version, and audit version, version number devtools indicates the developer tools, and the rest are official versions.
  • wx.request wx.uploadFile wx.downloadFile The maximum concurrency limit is 10.
  • wx.connectSocket The maximum concurrency limit is 5;
  • After the mini program enters the background, if the network request is not completed within 5s, the error message fail interrupted will be called back; before returning to the foreground, the network request interface call will not be called.

Return value encoding

  • It is recommended that the server return value use UTF-8 encoding. For non-UTF-8 encoding, the applet will try to convert it, but there is a possibility that the conversion will fail.
  • The mini program will automatically filter the BOM header (only one BOM header is filtered).

Callback function

As long as the server response is received successfully, no matter what the statusCode is, the success callback will be entered. Developers are requested to judge the return value according to the business logic.

Common Questions

HTTPS Certificate

Mini Programs must use HTTPS/WSS to initiate network requests. When the request is made, the system will verify the HTTPS certificate used by the server domain name. If the verification fails, the request cannot be successfully initiated. Due to system restrictions, different platforms have different strictness requirements for certificates. In order to ensure the compatibility of mini programs, it is recommended that developers configure certificates according to the highest standards and use relevant tools to check whether the existing certificates meet the requirements.

Requirements for certificates are as follows:

  • HTTPS certificates must be valid;
    • The certificate must be trusted by the system, that is, the root certificate has been built into the system
    • The domain name of the website where the SSL certificate is deployed must be consistent with the domain name issued by the certificate.
    • The certificate must be valid.
    • The certificate's trust chain must be complete (server configuration is required).
  • iOS does not support self-signed certificates
  • iOS certificates must meet Apple's App Transport Security (ATS) requirements.
  • TLS must support 1.2 and above. Some older Android models do not support TLS 1.2 yet. Please ensure that the HTTPS server's TLS version supports 1.2 and below.
  • Some CAs may not be trusted by the operating system. Developers should pay attention to the relevant notices of the mini program and each system when selecting a certificate.

TIP

  • The certificate validity can be verified using the openssl s_client -connect example.com:443 command, or other online tools can be used.
  • In addition to the network request API, if other HTTPS requests in the mini program are abnormal, please follow the above process to check, such as https images cannot be loaded, audio and video cannot be played, etc.

Skip domain name verification

In the developer tools, you can temporarily enable the option of not verifying the requested domain name, TLS version, and HTTPS certificate in the development environment to skip the verification of the server domain name. At this time, the server domain name will not be verified in the developer tools and when the mobile phone is in debug mode.

After the server domain name is successfully configured, it is recommended that developers turn off this option for development and test on various platforms to confirm that the server domain name is configured correctly

TIP

  • If the phenomenon of 'you can make requests when debugging mode is turned on, but you cannot make requests when debugging mode is turned off' appears on the mobile phone, please confirm whether the domain name verification has been skipped and whether the server domain name and certificate configuration are correct
  • If you encounter 'internal server error', please try the following methods to check yourself ①Change the browser ②Refresh the web page ③Check whether the file is placed in the correct server root directory Troubleshooting method: Splice the URL according to the following rules and try to access it yourself: https://m.da9c.cn/file name