All Products
Search
Document Center

Alibaba Cloud SDK:Use CommonRequest to call API operations

Last Updated:Sep 05, 2022

Note

If you want to call the API operations of an Alibaba 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 only to download the core package. You do not need to download and install SDKs of Alibaba Cloud services.

  2. Easy to use: You can call the newly released API operations of an Alibaba Cloud service without the need to update the SDK for the service.

  3. Fast iteration.

Use CommonRequest to call an API operation

Alibaba Cloud services provide RPC and RESTful APIs. The request method of CommonRequest varies based on the 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 invalid identifier, another API operation may be 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 more information about the values of these parameters, see the API reference in Documentation. You can also visit OpenAPI Explorer to obtain the parameter values of API operations for some services.

  • 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 of a service, such as 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 ACK clusters is /clusters and you want to send a CommonRequest request to call the RESTful API operation, 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:

using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
class Sample
{
    static void Main(string[] args)
    {
        // Create a client instance to initiate a request.
        IClientProfile profile = DefaultProfile.GetProfile(
            "<your-region-id>",
            "<your-access-key-id>",
            "<your-access-key-secret>");
        DefaultAcsClient client = new DefaultAcsClient(profile);
        try
        {
            // Construct a request.
            CommonRequest request = new CommonRequest();
            request.Domain = "ecs.aliyuncs.com";
            request.Version = "2014-05-26";
            // Specify the value of the ApiName (Action) field for the RPC API operation.
            request.Action = "DescribeInstanceStatus";
            request.AddQueryParameters("PageNumber", "1");
            request.AddQueryParameters("PageSize", "30");
            // Initiate the request and receive the response.
            CommonResponse response = client.GetCommonResponse(request);
            System.Console.WriteLine(response.Data);
        }
        catch (ServerException ex)
        {
            System.Console.WriteLine(ex.ToString());
        }
        catch (ClientException ex)
        {
            System.Console.WriteLine(ex.ToString());
        }
    }
}

Example: Call a RESTful API operation

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

using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
class Sample
{
    static void Main(string[] args)
    {
        // Create a client instance to initiate a request.
        IClientProfile profile = DefaultProfile.GetProfile(
            "<your-region-id>",
            "<your-access-key-id>",
            "<your-access-key-secret>");
        DefaultAcsClient client = new DefaultAcsClient(profile);
        try
        {
            // Construct a request.
            CommonRequest request = new CommonRequest();
            request.Domain = "cs.aliyuncs.com";
            request.Version = "2015-12-15";
            // Specify the value of the UriPattern field for the Restful API operation.
            request.UriPattern = "/clusters";
            // Initiate the request and receive the response.
            CommonResponse response = client.GetCommonResponse(request);
            System.Console.WriteLine(response.Data);
        }
        catch (ServerException ex)
        {
            System.Console.WriteLine(ex.ToString());
        }
        catch (ClientException ex)
        {
            System.Console.WriteLine(ex.ToString());
        }
    }
}