All Products
Search
Document Center

Alibaba Cloud SDK:CommonRequest

Last Updated:Feb 11, 2023

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 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:

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 the instance.
        DefaultProfile profile = DefaultProfile.getProfile(
            "<your-region-id>",          // The ID of the region.
            "<your-access-key-id>",      // The AccessKey ID.
            "<your-access-key-secret>"); // The AccessKey secret.
        IAcsClient client = new DefaultAcsClient(profile);
        // Create an API request and set the request 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 operation

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

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 the instance.
        DefaultProfile profile = DefaultProfile.getProfile(
            "<your-region-id>",          // The ID of the region.
            "<your-access-key-id>",      // The AccessKey ID.
            "<your-access-key-secret>"); // The AccessKey secret.
        IAcsClient client = new DefaultAcsClient(profile);
        // Create an API request and set the request 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();
        }
    }
}