Proxy configuration
The V2.0 Go SDK supports HTTP, HTTPS, and SOCKS5 proxies. You can configure proxies through environment variables, Config, or RuntimeOptions.
Proxy types
The SDK supports HTTP, HTTPS, and SOCKS5 proxies. Use only one proxy type at a time.
|
Proxy type |
Description |
|
HTTP proxy |
Set httpProxy to the proxy address in the format |
|
HTTPS proxy |
Set httpsProxy to the proxy address in the format |
|
SOCKS5 proxy |
Set socks5Proxy to the proxy address in the format |
If the proxy requires authentication, include the username and password in the URL: http://<user>:<password>@<IP address>:<port> or socks5://<user>:<password>@<IP address>:<port>.
For HTTP or HTTPS proxies, use noProxy to specify comma-separated addresses (domain names or IPs) that bypass the proxy.
Configuration methods
Priority order (highest to lowest): RuntimeOptions > Config > environment variables.
-
Configure the proxy with environment variables.
NoteSOCKS5 proxy does not support environment variable configuration.
-
HTTP_PROXYorhttp_proxy: the HTTP proxy address. -
HTTPS_PROXYorhttps_proxy: the HTTPS proxy address. -
NO_PROXYorno_proxy: addresses that bypass the proxy.
-
-
Configure the proxy in RuntimeOptions. This applies only to requests that use these runtime parameters.
import ( util "github.com/alibabacloud-go/tea-utils/v2/service" openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" "github.com/alibabacloud-go/tea/tea" ) config := &openapi.Config{ Protocol: tea.String("https"), } runtime := &util.RuntimeOptions{ // Modify the request protocol (Protocol) as needed. httpsProxy is effective only for the HTTPS protocol, and httpProxy is effective only for the HTTP protocol. HttpProxy: tea.String("http://127.0.0.1:9898"), HttpsProxy: tea.String("http://127.0.0.1:8989"), NoProxy: tea.String("127.0.0.1,localhost"), // SOCKS5 proxy // Socks5Proxy: tea.String("socks5://127.0.0.1:8989"), // Socks5NetWork: tea.String("tcp"), } -
Configure the proxy in Config when initializing the client. This applies to all requests.
Example:
import ( openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" "github.com/alibabacloud-go/tea/tea" ) config := &openapi.Config{ // httpsProxy is effective only for the HTTPS protocol, and httpProxy is effective only for the HTTP protocol. Protocol: tea.String("https"), // HTTP proxy HttpProxy: tea.String("http://127.0.0.1:9898"), // HTTPS proxy HttpsProxy: tea.String("http://127.0.0.1:8989"), NoProxy: tea.String("127.0.0.1,localhost"), // SOCKS5 proxy // Socks5Proxy: tea.String("socks5://127.0.0.1:8989"), // Socks5NetWork: tea.String("tcp"), }