Examples of calling an API

更新时间:
复制 MD 格式

API Gateway lets you call API services from other Alibaba Cloud users or third-party service providers. API Gateway also provides a range of management and support services.

Call an API

You can use the multi-language SDKs provided in the API Gateway console to call an API (Download SDKs and documents). You can also write your own HTTP or HTTPS requests to call the API.

The following sections describe API calls and provide examples:

  1. Request

    A request URL consists of a domain name and a path.

    Use the domain name that is bound to the API group. If a domain name is not bound to the group, use the second-level domain name of the group.

    http://e710888d3ccb4638a723ff8d03837095-cn-qingdao.aliapi.com/demo/post

    Request method:

    POST

    Request body:

    FormParam1=FormParamValue1&FormParam2=FormParamValue2   # HTTP Request Body

    Request header:

    Host: e710888d3ccb4638a723ff8d03837095-cn-qingdao.aliapi.com
    Date: Mon, 22 Aug 2016 11:21:04 GMT
    User-Agent: Apache-HttpClient/4.1.2 (java 1.6)
    Content-Type: application/x-www-form-urlencoded; charset=UTF-8 
    # The type of the request body. Set this based on the actual content of the request body.
    
    Accept: application/json 
    # The type of the response body. Some APIs return data in a specific format based on the specified response type. Manually specify this request header. If this header is not specified, some HTTP clients use the default value */*, which may cause a signature error.
    
    X-Ca-Request-Mode: debug 
    # Specifies whether to enable debug mode. This parameter is not case-sensitive. If this parameter is not specified, debug mode is disabled by default. Enable this mode during the API debugging phase.
    
    X-Ca-Version: 1  
    # The version number of the API. All APIs support only version 1. You can leave this request header unspecified. The default version number is 1.
    
    X-Ca-Signature-Headers: X-Ca-Request-Mode,X-Ca-Version,X-Ca-Stage,X-Ca-Key,X-Ca-Timestamp 
    # The custom request headers that are included in the signature calculation. The server reads the request headers based on this configuration to sign the request. This configuration does not include the Content-Type, Accept, Content-MD5, and Date request headers. These headers are already included in the basic signature structure. For more information, see the request signature documentation.
    
    X-Ca-Stage: RELEASE  
    # The stage of the API. Valid values: TEST, PRE, and RELEASE. This parameter is not case-sensitive. The API provider can select the stage to which the API is published. The API can be called only after it is published to the specified stage. Otherwise, the system reports that the API is not found or the URL is invalid.
    
    X-Ca-Key: 60022326  
    # The AppKey of the request. Generate the AppKey in the API Gateway console. An API can be called only after it is authorized. By default, APIs purchased from channels such as Alibaba Cloud Marketplace are already authorized for the app. All Alibaba Cloud products share one AppKey system. Delete an AppKey with caution to avoid affecting other activated Alibaba Cloud products.
    
    X-Ca-Timestamp: 1471864864235 
    # The timestamp of the request. This value is the number of milliseconds that have elapsed since 00:00:00 UTC on January 1, 1970. The timestamp is valid for 15 minutes.
    
    X-Ca-Nonce:b931bc77-645a-4299-b24b-f3669be577ac  
    # The unique ID of the request. The combination of AppKey, API, and Nonce must be unique within 15 minutes. This parameter is used with the timestamp to prevent replay attacks.
    
    X-Ca-Signature: FJleSrCYPGCU7dMlLTG+UD3Bc5Elh3TV3CWHtSKh1Ys= 
    # The request signature.
    
    CustomHeader: CustomHeaderValue  
    # A custom request header. This is only an example. In an actual request, you can set multiple custom request headers based on the API definition.
  2. Response

    Status code:

    400  
    # The status code of the response. A value from 200 to 299 indicates success. A value from 400 to 499 indicates a client error. A value of 500 or greater indicates a server error.

    Response header:

    X-Ca-Request-Id: 7AD052CB-EE8B-4DFD-BBAF-EFB340E0A5AF 
    # The unique ID of the request. After a request enters API Gateway, API Gateway generates a request ID and returns it to the client in the response header. Record this request ID on both the client and the backend service for troubleshooting and tracking.
    
    X-Ca-Error-Message: Invalid Url  
    # The error message returned by API Gateway. If an error occurs in a request, API Gateway returns the error message to the client in a response header.
    
    X-Ca-Debug-Info: {"ServiceLatency":0,"TotalLatency":2}  
    # If debug mode is enabled, debug information is returned. This information may change and is for reference only during the joint debugging phase.
                            
  • When you call an API, the request must include signature information, regardless of whether you use HTTP or HTTPS. The AppKey identifies you, and the AppSecret is the key used to encrypt the signature string and verify the signature on the server. For more information about how to calculate and pass the encrypted signature, see Use digest authentication to call an API.

  • For sample code on how to calculate signatures, see the SDK examples on the Download SDKs page in the API Gateway console.

  • For more information, see Overview.