すべてのプロダクト
Search
ドキュメントセンター

API Gateway:呼び出しAPIの例

最終更新日:Jul 31, 2024

API Gatewayを使用して、他のAlibaba Cloudユーザーまたはサードパーティのサービスプロバイダーが有効にしているAPIサービスを呼び出すことができます。 API Gatewayは、一連の管理およびサポートサービスを提供します。

APIの呼び出し

API Gatewayコンソールで提供されている複数のプログラミング言語のSDKを使用して、APIを呼び出すことができます。 SDKの詳細については、API GatewayコンソールのSDKページを参照してください。 APIを呼び出すHTTPまたはHTTPSリクエストを作成することもできます。

次の項目では、API呼び出しの詳細と例を説明します。

1. リクエスト

リクエストURLは、ドメイン名とパスで構成されます。

ドメイン名は、呼び出されるAPIを含むAPIグループにバインドされている必要があります。 ドメイン名がAPIグループにバインドされていない場合は、システムによって割り当てられた第2レベルのドメイン名を使用できます。

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

サンプル要求方法:

POST

サンプルリクエスト本文:

FormParam1=FormParamValue1&FormParam2=FormParamValue2   # HTTP Request Body

サンプル要求ヘッダー:

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. Specify the request body type based on the actual request you want to make. 

Accept: application/json 
# The type of the response body. Some APIs can return data in the appropriate format based on the specified response type. We recommend that you manually specify the request header. If the request header is not specified, some HTTP clients use the default value */*. This may cause signature errors. 

X-Ca-Request-Mode: debug 
# Specifies whether to enable the debug mode. This parameter is not case-sensitive. If it is not specified, the debug mode is disabled. Enable this mode in 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 involved in 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 because these headers are included in the basic signature structure. For information about the signature, see Request signatures. 

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 prompts that the API cannot be found or that the request URL is invalid. 

X-Ca-Key: 60022326  
# The AppKey of the request. You must obtain the AppKey in the API Gateway console. Applications can call APIs only after they are granted the permissions. By default, APIs that are purchased in Alibaba Cloud Marketplace have granted the permissions to applications. All Alibaba Cloud services use the same AppKey system. To prevent the impact on other Alibaba Cloud services that you activated, proceed with caution when you delete an AppKey. 

X-Ca-Timestamp: 1471864864235 
# The timestamp of the request. This value is a UNIX timestamp that represents the number of milliseconds that have elapsed since January 1, 1970 00:00:00 UTC. By default, the timestamp is valid for 15 minutes. 

X-Ca-Nonce:b931bc77-645a-4299-b24b-f3669be577ac  
# The unique ID of the request. AppKey, API, and Nonce must be unique within the last 15 minutes. To prevent replay attacks, you must specify both the X-Ca-Nonce header and the X-Ca-Timestamp header. 

X-Ca-Signature: FJleSrCYPGCU7dMlLTG+UD3Bc5Elh3TV3CWHtSKh1Ys= 
# The signature of the request. 

CustomHeader: CustomHeaderValue  
# The custom request headers. CustomHeaderValue is used as an example. You can configure multiple custom request headers in a request based on the definition of the API that is being called.

2. 応答

ステータスコード:

400  
# The status code of the response. If the value is greater than or equal to 200 but less than 300, the call succeeded. If the value is greater than or equal to 400 but less than 500, a client-side error has occurred. If the value is greater than 500, a server-side error has occurred.

Responseヘッダー:

X-Ca-Request-Id: 7AD052CB-EE8B-4DFD-BBAF-EFB340E0A5AF 
# The unique ID of the request. When API Gateway receives a request, it generates a request ID and returns the request ID to the client in the X-Ca-Request-Id header. We recommend that you record the request ID in the client and backend server for troubleshooting and tracking. 

X-Ca-Error-Message: Invalid Url  
# The error message returned by API Gateway. If a request fails, API Gateway returns the error message to the client in the X-Ca-Error-Message header. 

X-Ca-Debug-Info: {"ServiceLatency":0,"TotalLatency":2}  
# The message returned only when the debug mode is enabled. The message is used only for reference at the debugging stage.
                        

HTTPまたはHTTPSのどちらを使用してAPIを呼び出しても、リクエストには署名情報が含まれている必要があります。 署名のAppKeyはIDの検証に使用され、署名のAppSecretは署名文字列の暗号化と検証に使用されます。 暗号化された署名を計算して渡す方法については、「ダイジェスト認証を使用してAPIを呼び出す」をご参照ください。

署名計算のデモについては、API GatewayコンソールのSDKページをご参照ください。

詳細は、概要をご参照ください。