All Products
Search
Document Center

Alibaba Cloud SDK:Use CommonRequest

Last Updated:Apr 06, 2022

Note

If you want to call the API operations of a cloud service that does not provide SDKs, you can use CommonRequest. You can use CommonRequest to call API operation operations.

Benefits

CommonRequest provides the following benefits:

  1. Lightweight: You need to download only the core package. You do not need to download and then install an SDK.

  2. Easy to use: You can call the newly released API operations without the need to update the SDK.

  3. Fast iteration.

Use CommonRequest to call an API operation

Alibaba Cloud provides RPC and RESTful APIs. The request method of CommonRequest varies based on the architectural style of the API that you want to call.

In most cases, an API uses the RPC style if the API operations include the Action parameter. An API uses the RESTful style if the API operations include the PathPattern parameter. All API operations of a service use the same API style. The API of each service supports only one style. If you pass an incorrect identifier, another API operation is called, or the ApiNotFound error is returned.

If you want to send a CommonRequest request, you must obtain the values of the following parameters. For information about the values of these parameters, see the API reference in Documentation. For information about the parameter values of an API, visit OpenAPI Explorer.

  • Domain: the endpoint of a service.

  • Version: the version of the API. The version is in the YYYY-MM-DD format.

  • Operation information: the name of the API operation that you want to call.

    • The APIs of most Alibaba Cloud services, such as Elastic Compute Service (ECS) and ApsaraDB RDS, are RPC APIs. If you want to call an RPC API operation, you must obtain the value of the Action parameter, and then specify the value in the request.ApiName = "<Action>" format.

      • For example, if you want to use CommonRequest to call the RunInstances operation, specify the operation name in the request.ApiName = "RunInstances" format.

    • If you want to call a RESTful API operation, for example, an operation for Container Service for Kubernetes (ACK), you must obtain the value of the PathPattern parameter, and then specify the RESTful path in the request.PathPattern = "<PathPattern>" format.

      • For example, if the value of the PathPattern parameter of the API operation that is used to query all clusters of ACK is /clusters and you want to send a CommonRequest request, you must specify the RESTful path in the request.PathPattern = "/clusters" format.

Example: Call an RPC API operation

The following code shows how to use CommonRequest to call the DescribeInstanceStatus operation of ECS:

package main
import (
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "fmt"
)
func main() {
    client, err := sdk.NewClientWithAccessKey("cn-hangzhou", "{your_access_key_id}", "{your_access_key_secret}")
    if err != nil {
        panic(err)
    }
    request := requests.NewCommonRequest()
    request.Domain = "ecs.aliyuncs.com"
    request.Version = "2014-05-26"
    // This is an RPC API. Therefore, the ApiName(Action) field must be specified.
    request.ApiName = "DescribeInstanceStatus"
    request.QueryParams["PageNumber"] = "1"
    request.QueryParams["PageSize"] = "30"
    response, err := client.ProcessCommonRequest(request)
    if err != nil {
        panic(err)
    }
    fmt.Print(response.GetHttpContentString())
}

Example: Call a RESTful API operation

The following code shows how to use CommonRequest to call an API operation of ACK to query all cluster instances:

package main
import (
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "fmt"
)
func main() {
    client, err := sdk.NewClientWithAccessKey("cn-hangzhou", "{your_access_key_id}", "{your_access_key_secret}")
    if err != nil {
        panic(err)
    }
    request := requests.NewCommonRequest()
    request.Domain = "cs.aliyuncs.com"
    request.Version = "2015-12-15"
    // This is a RESTful API. Therefore, the PathPattern field must be specified.
    request.PathPattern = "/clusters"
    response, err := client.ProcessCommonRequest(request)
    if err != nil {
        panic(err)
    }
    fmt.Print(response.GetHttpContentString())
}