When you use a proxy server to send requests, you must correctly configure the proxy. This topic describes how to configure a proxy in Alibaba Cloud SDK V1.0 for Go. This ensures that your request data can be transmitted as expected by using the proxy server.
Configuration method
The proxy configurations take effect in the following descending order: the configuration when you initialize an SDK client and the configuration by using environment variables.
The following code provides an example on how to configure a proxy when you initialize an SDK client.
import ( "fmt" "os" "github.com/aliyun/alibaba-cloud-sdk-go/sdk" "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials" ecs "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs" ) func main() { // Specify runtime parameters. config := sdk.NewConfig() // Use the AccessKey ID and AccessKey secret of the Resource Access Management (RAM) user. credential := credentials.NewAccessKeyCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")) client, err := ecs.NewClientWithOptions("cn-hangzhou", config, credential) if err != nil { panic(err) } // Configure a proxy when you initialize an SDK client. client.SetHttpsProxy("http://user:password@127.0.0.1:8989") // Configure an HTTPS proxy. client.SetHttpProxy("http://127.0.0.1:8080") // Configure an HTTP proxy. client.SetNoProxy("127.0.0.1,localhost") // Configure the IP addresses or domain names that do not require proxies. // Create a request. request := ecs.CreateDescribeRegionsRequest() request.Scheme = "https" // The request parameters. request.InstanceChargeType = "PrePaid" // The billing method of the instance. request.ResourceType = "instance" // The resource type. // Send the request and obtain a response. response, err := client.DescribeRegions(request) if err != nil { fmt.Print(err.Error()) } fmt.Printf("response is %#v\n", response) }You can also use the following environment variables to configure a proxy:
HTTP_PROXY or http_proxy. Example of a valid value: http://127.0.0.1:8080.
HTTPS_PROXY or https_proxy. Set the environment variable in the following format: http://<user>:<password>@127.0.0.1:8989.
NO_PROXY or no_proxy. Set the environment variable in the following format: 127.0.0.1,localhost.