If an Alibaba Cloud product does not provide an SDK for its APIs, you can use the generic calling method (CommonRequest) to call the product APIs. By using the CommonRequest calling method, you can call any API.

Characteristics of CommonRequest calling

The CommonRequest calling method has the following characteristics:

  1. Light weight: You only need to download the core package, and do not need to download and install all product SDKs.
  2. Easy and convenient: You can use the latest API without updating the SDK.
  3. Fast iteration: Updates are made often and are made available quickly.

Call an API using CommonRequest

The APIs of Alibaba Cloud products can be classified into two types, RPC and RESTful styles. The method of making a CommonRequest request varies based on the specific API type.

In general, the API required by the Action parameter belongs to the RPC type and the API required by the PathPattern parameter belongs to the RESTful type. In general, all APIs in a product are of the same type. Each API only supports calling of a specific type. Entering wrong codes may call other APIs or receive the error message of ApiNotFound.

To make a CommonRequest request, you must obtain the values for the following parameters. You can obtain the values of these parameters from the API documents at the Document Center. Besides, you can obtain the parameters of an API through OpenAPI Explorer.

  • Domain: The universal domain name of a product.

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

    You can get the API version from the common parameters in the API document of a product.

  • API information: The name of the API to call.
    • If an API is an RPC API, such as ECS and RDS, you must obtain the value of the Action parameter and specify the API to call in the form of request.ApiName = "<Action>"

      For example, if the Action value of the ECS-RunInstances API is RunInstances, you can use request.ApiName = "RunInstances" to specify the API to call when making a CommonRequest request.

    • If an API is a RESTful API, such as Container Service, you must obtain the value of the PathPattern parameter and specify the RESTful path to call in the form of request.PathPattern = "<PathPattern>".

      For example, the PathPattern value of the CS-GetClusterList API is /clusters, you can use request.PathPattern = "/clusters" to specify the RESTful path when making a CommonRequest request.

Example: Call an RPC API

The following code shows how to use the CommonRequest calling method to call the DescribeInstanceStatus API:
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
public class Sample {
    public static void main(String[] args) {
        //Create a DefaultAcsClient instance and initialize it.
        DefaultProfile profile = DefaultProfile.getProfile(
            "<your-region-id>", // your region ID
            "<your-access-key-id>", // your AccessKey ID
            "<your-access-key-secret>"); // your AccessKey Secret
        IAcsClient client = new DefaultAcsClient(profile);
        // Create an API request and set parameters
        CommonRequest request = new CommonRequest();
        request.setDomain("ecs.aliyuncs.com");
        request.setVersion("2014-05-26");
        request.setAction("DescribeInstanceStatus");
        request.putQueryParameter("PageNumber", "1");
        request.putQueryParameter("PageSize", "30");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    

Example: Call a RESTful API

The following code shows how to use commonrequest to call the Container Service's view of all cluster instance interfaces:
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
public class Sample {
    public static void main(String[] args) {
        //Create a DefaultAcsClient instance and initialize it.
        DefaultProfile profile = DefaultProfile.getProfile(
            "<Your-region-ID>", // your region ID
            "<Your-access-key-ID>", // your AccessKey ID
            "<Your-access-key-secret>"); // your AccessKey Secret
        IAcsClient client = new DefaultAcsClient(profile);
        // Create an API request and set parameters
        CommonRequest request = new CommonRequest();
        request.setDomain("cs.aliyuncs.com");
        request.setVersion("2015-12-15");
        request.setUriPattern("/clusters");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();