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.

Benefits

CommonRequest has the following benefits:

  1. Lightweight: You only need to download 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 updating the SDK.
  3. Fast iteration.

Use CommonRequest to call an API operation

The APIs of Alibaba Cloud services use two styles: RPC and RESTful. The method of making a CommonRequest request varies based on the API style.

In most cases, an API uses the RPC style if its API operations include the Action parameter. An API uses the RESTful style if its API operations include the PathPattern parameter. Typically, all API operations of a service uses the same API style. Each service API only supports one style. If you pass the wrong identifier, another API operation may be called or the ApiNotFound error may be returned.

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 reference in Documentation. You can also obtain the parameter values of an API from OpenAPI Explorer.

  • Domain: the endpoint of a service.
  • Version: the version of the API, in the format of YYYY-MM-DD.

    You can find the API version of a service in the Common Parameters section of the API reference.

  • Operation information: the name of the API operation that you want to call.
    • If you want to call an RPC API (the APIs of most Alibaba Cloud services are RPC APIs, such as ECS and ApsaraDB for RDS), you must obtain the value of the Action parameter and specify the value in the request.ApiName = "<Action>".

      For example, to use the CommonRequest method to call the RunInstances operation, you must specify the operation name by using request.ApiName = "RunInstances".

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

      For example, the PathPattern parameter of the API operation that is used to query all cluster instances is set to /clusters. When you initiate a CommonRequest request, you must specify the RESTful path in the request.PathPattern = "/clusters" format.

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();