All Products
Search
Document Center

Alibaba Cloud SDK:Use CommonRequest to call API operations

Last Updated:Jun 25, 2024

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 all API operations.

Benefits

CommonRequest provides the following benefits:

  1. Lightweight architecture: You need to only download the core package. You do not need to download and install the SDKs of services.

  2. Ease of use: You can call the newly released API operations of a service without the need to update the SDK of the service.

  3. Fast iteration.

Use CommonRequest to call an API operation

Alibaba Cloud services provide remote procedure call (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, such as 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 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.

Call an RPC API operation

The following sample code provides an example on 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(
            // The region ID.
            "<your-region-id>",
            // Obtain the AccessKey ID of a Resource Access Management (RAM) user from environment variables.
            Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
            // Obtain the AccessKey secret of the RAM user from environment variables.
            Environment.GetEnvironmentVariable("ALIBABA_CLOUD_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) parameter for the RPC API operation.
            request.Action = "DescribeInstanceStatus";
            request.AddQueryParameters("PageNumber", "1");
            request.AddQueryParameters("PageSize", "30");
            // Initiate the request and obtain 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());
        }
    }
}

Call a RESTful API operation

The following sample code provides an example on 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(
            // The region ID.
            "<your-region-id>",
            // Obtain the AccessKey ID of the RAM user from environment variables.
            Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
            // Obtain the AccessKey secret of the RAM user from environment variables.
            Environment.GetEnvironmentVariable("ALIBABA_CLOUD_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 parameter for the Restful API operation.
            request.UriPattern = "/clusters";
            // Initiate the request and obtain 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());
        }
    }
}