All Products
Search
Document Center

Alibaba Cloud SDK:Configure an HTTPS request

Last Updated:Jul 09, 2024

This topic describes how to configure an HTTPS request in Alibaba Cloud SDK V2.0 for Go.

Alibaba Cloud SDK V2.0 for Go allows you to specify a protocol over which API requests are sent for an SDK client in the Config object. We recommend that you specify HTTPS as the protocol to improve the security of data transmission. If you do not specify the protocol, API requests are sent over HTTPS by default.

config := &openapi.Config{
    // Specify HTTPS or HTTP as the protocol.
    Protocol: tea.String("HTTPS"),
}
Important

By default, if you send an API request over HTTPS, the SDK enables certificate verification to verify the validity of SSL/TLS certificates. If no certificate is configured in the development environment, an error that indicates the certificate verification fails is reported.

To ensure network communication security, we recommend that you enable certificate verification. If certificate verification must be disabled in the test environment, you can set the IgnoreSSL parameter to true when you specify runtime parameters.

// Create a RuntimeOptions instance and specify runtime parameters. 
runtime := &util.RuntimeOptions{}
// Ignore SSL certificate-related errors.
runtime.IgnoreSSL = tea.Bool(true)

The following sample code provides an example on how to configure an HTTPS request.

package main

import (
	"encoding/json"
	"fmt"
	"github.com/aliyun/credentials-go/credentials"

	openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	ecs20140526 "github.com/alibabacloud-go/ecs-20140526/v4/client"
	util "github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
)

func main() {
	// Initialize the Credentials client.
	credential, err := credentials.NewCredential(nil)
	if err != nil {
		panic(err)
	}
	config := &openapi.Config{
		// Configure a credential.
		Credential: credential,
		// Specify HTTPS or HTTP as the protocol.
		Protocol: tea.String("HTTPS"),
		RegionId: tea.String("<RegionId>"),
	}
	client, err := ecs20140526.NewClient(config)
	if err != nil {
		panic(err)
	}
	describeRegionsRequest := &ecs20140526.DescribeRegionsRequest{}
	// Create a RuntimeOptions instance and specify runtime parameters. 
	runtime := &util.RuntimeOptions{}
	// Ignore SSL certificate-related errors.
	runtime.IgnoreSSL = tea.Bool(true)
	resp, err := client.DescribeRegionsWithOptions(describeRegionsRequest, runtime)
	if err != nil {
		panic(err)
	}
	// The response, which contains the body and headers that are returned by the server.
	body, err := json.Marshal(resp.Body)
	if err != nil {
		panic(err)
	}
	fmt.Printf("body: %s\n", string(body))
}