All Products
Search
Document Center

Alibaba Cloud SDK:Configure an HTTPS request

Last Updated:Apr 28, 2024

This topic describes how to configure an HTTPS request in Alibaba Cloud Darabonba SDK.

Alibaba Cloud Darabonba SDK allows you to specify the protocol over which API requests are sent for an SDK client. We recommend that you send API requests over HTTPS. 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 SSL/TLS 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{}
// Disable certificate verification to ignore SSL/TLS 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"

    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 credentials.
        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{}
    // Disable certificate verification to ignore SSL/TLS 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))
}