Obtain the request ID

Updated at:
Copy as MD

The following examples use the GetProject operation to show how to obtain the request ID of a Simple Log Service request in Java, Python, and Go.

Java

When a code execution error occurs, you can call exp.getRequestId() to obtain the request ID.

  • Sample code

    import com.aliyun.openservices.log.Client;
    import com.aliyun.openservices.log.exception.LogException;
    import com.aliyun.openservices.log.response.GetProjectResponse;
    
    public class GetProject {
        public static void main(String[] args) throws LogException {
            // In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
            String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
            String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
            // Enter the project name.
            String projectName = "aliyun-test-project";
            // Specify a Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the Singapore region is used. Replace the parameter value with the actual endpoint.
            String host = "https://ap-southeast-1.log.aliyuncs.com";
    
            // Create a Simple Log Service client.
            Client client = new Client(host, accessId, accessKey);
            try {
                System.out.println("ready to get project");
                // Query the specified project.
                GetProjectResponse response = client.GetProject(projectName);
                System.out.println(String.format("get project %s success",projectName));
                // Obtain the RequestId.
                System.out.println(response.GetRequestId());
    
            } catch (LogException e) {
                System.out.println("LogException e :" + exp.getRequestId());
            }
        }
    }
  • Output

    67F8B986CFECBABE7BCF****

Python

When a code execution error occurs, you can call error.get_request_id() to obtain the request ID.

  • Sample code

    from aliyun.log import LogClient
    import os
    
    # In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
    access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
    access_key_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
    
    # Specify a Simple Log Service endpoint.
    endpoint = "ap-southeast-1.log.aliyuncs.com"
    
    client = LogClient(endpoint, access_key_id, access_key_secret)
    
    # Enter the project name.
    project_name = "aliyun-test-project"
    
    
    # Obtain information about the specified project.
    def main():
        try:
            res = client.get_project(project_name)
            print(res.get_request_id())
        except Exception as error:
            print(error.get_request_id())
    
    
    if __name__ == '__main__':
        main()
    
  • Output

    67F8B986CFECBABE7BCF****

Go

When a code execution error occurs, the request ID is included in the output.

  • Sample code

    package main
    import (
    	"fmt"
    	"os"
    
    	sls "github.com/aliyun/aliyun-log-go-sdk"
    )
    
    func main() {
    	// Specify a Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the Singapore region is used. Replace the parameter value with the actual endpoint.
    	Endpoint := "ap-southeast-1.log.aliyuncs.com"
    
    	// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
    	AccessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
    	AccessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
    	// The Security Token Service (STS) token of the Resource Access Management (RAM) user. If you leave this parameter empty, no STS tokens are used.
    	SecurityToken := ""
    	// Create a Simple Log Service client.
    	provider := sls.NewStaticCredentialsProvider(AccessKeyId, AccessKeySecret, SecurityToken)
    	client := sls.CreateNormalInterfaceV2(Endpoint, provider)
    
    	// View the project.
    	name := "ali-test-project"
    	project, err := client.GetProject(name)
    	if err != nil {
    		panic(err)
    	}
    	fmt.Println(project.Description)
    }
  • Output

    panic: {
        "httpCode": 401,
        "errorCode": "Unauthorized",
        "errorMessage": "The project does not belong to you.",
        "requestID": "67F8CBEDE549DF37E48E****"
    }