在 APIs Gateway 中開發 REST API
在 Amazon API Gateway 中,您可以將 REST API 建置為可程式化實體的集合,稱為 API Gateway 資源。例如,您可以使用 RestApi 資源來代表可包含 Resource 實體集合的 API。
每個 Resource
實體可以擁有一或多個 Method 資源。Method
是用戶端提交的傳入請求,可包含下列請求參數:路徑參數、標頭或查詢字串參數。此外,根據 HTTP 方法而定,請求可以包含內文。您的方法會定義用戶端存取公開 Resource
的方式。若要將 Method
與後端端點整合,也稱為整合端點,您可以建立整合資源。這會將傳入請求轉送到指定的整合端點 URI。如有必要,您可以轉換請求參數或請求內文以符合後端需求,也可以建立代理整合,其中 API Gateway 會以標準化格式將整個請求傳送至整合端點 URI,然後直接將回應傳送至用戶端。
針對回應,您可以建立 MethodResponse 資源來代表用戶端接收的回應,並建立 IntegrationResponse 資源來代表後端傳回的回應。請先使用整合回應來轉換後端回應資料,再將資料傳回至用戶端,或將後端回應依現狀傳遞到用戶端。
REST API 的範例資源
下圖顯示 API Gateway 如何針對 GET /pets
資源的 HTTP 代理和 HTTP 非代理整合,實作此請求/回應模型。用戶端會將 x-version:beta
標頭傳送至 API Gateway,而 API Gateway 會將 204
狀態碼傳送至用戶端。
在非代理整合中,API Gateway 會修改整合請求和整合回應來執行資料轉換,以滿足後端需求。在非代理整合中,您可以在方法請求中存取內文,但會在整合請求中進行轉換。當整合端點傳回具有內文的回應時,您可以在整合回應中存取和轉換內文。您無法在方法回應中修改內文。
在代理整合中,整合端點會修改請求和回應。API Gateway 不會修改整合請求或整合回應,且會將傳入的請求依原樣傳送至後端。
無論整合類型為何,用戶端都會將請求傳送至 API Gateway,而 API Gateway 會同步回應。
- Non-proxy integration
-
- Proxy integration
下列範例執行日誌顯示 API Gateway 在上一個範例中記錄的內容。部分值和初始日誌已移除,以便清楚呈現內容:
- Non-proxy integration
-
Wed Feb 12 23:56:44 UTC 2025 : Starting execution for request: abcd-1234-5678
Wed Feb 12 23:56:44 UTC 2025 : HTTP Method: GET, Resource Path: /pets
Wed Feb 12 23:56:44 UTC 2025 : Method request path: {}
Wed Feb 12 23:56:44 UTC 2025 : Method request query string: {}
Wed Feb 12 23:56:44 UTC 2025 : Method request headers: {x-version=beta}
Wed Feb 12 23:56:44 UTC 2025 : Method request body before transformations:
Wed Feb 12 23:56:44 UTC 2025 : Endpoint request URI: http://petstore-demo-endpoint.execute-api.com/petstore/pets
Wed Feb 12 23:56:44 UTC 2025 : Endpoint request headers: {app-version=beta}
Wed Feb 12 23:56:44 UTC 2025 : Endpoint request body after transformations:
Wed Feb 12 23:56:44 UTC 2025 : Sending request to http://petstore-demo-endpoint.execute-api.com/petstore/pets
Wed Feb 12 23:56:45 UTC 2025 : Received response. Status: 200, Integration latency: 123 ms
Wed Feb 12 23:56:45 UTC 2025 : Endpoint response headers: {Date=Wed, 12 Feb 2025 23:56:45 GMT}
Wed Feb 12 23:56:45 UTC 2025 : Endpoint response body before transformations:
Wed Feb 12 23:56:45 UTC 2025 : Method response body after transformations: (null)
Wed Feb 12 23:56:45 UTC 2025 : Method response headers: {X-Amzn-Trace-Id=Root=1-abcd-12345}
Wed Feb 12 23:56:45 UTC 2025 : Successfully completed execution
Wed Feb 12 23:56:45 UTC 2025 : Method completed with status: 204
- Proxy integration
Wed Feb 12 23:59:42 UTC 2025 : Starting execution for request: abcd-1234-5678
Wed Feb 12 23:59:42 UTC 2025 : HTTP Method: GET, Resource Path: /pets
Wed Feb 12 23:59:42 UTC 2025 : Method request path: {}
Wed Feb 12 23:59:42 UTC 2025 : Method request query string: {}
Wed Feb 12 23:59:42 UTC 2025 : Method request headers: {x-version=beta}
Wed Feb 12 23:59:42 UTC 2025 : Method request body before transformations:
Wed Feb 12 23:59:42 UTC 2025 : Endpoint request URI: http://petstore-demo-endpoint.execute-api.com/petstore/pets
Wed Feb 12 23:59:42 UTC 2025 : Endpoint request headers: { x-version=beta}
Wed Feb 12 23:59:42 UTC 2025 : Endpoint request body after transformations:
Wed Feb 12 23:59:42 UTC 2025 : Sending request to http://petstore-demo-endpoint.execute-api.com/petstore/pets
Wed Feb 12 23:59:43 UTC 2025 : Received response. Status: 204, Integration latency: 123 ms
Wed Feb 12 23:59:43 UTC 2025 : Endpoint response headers: {Date=Wed, 12 Feb 2025 23:59:43 GMT}
Wed Feb 12 23:59:43 UTC 2025 : Endpoint response body before transformations:
Wed Feb 12 23:59:43 UTC 2025 : Method response body after transformations:
Wed Feb 12 23:59:43 UTC 2025 : Method response headers: {Date=Wed, 12 Feb 2025 23:59:43 GMT}
Wed Feb 12 23:59:43 UTC 2025 : Successfully completed execution
Wed Feb 12 23:59:43 UTC 2025 : Method completed with status: 204
若要匯入類似的 API 並在 AWS Management Console 中進行測試,請參閱範例 API。
用於開發的其他 REST API 功能
API Gateway 支援其他用於開發 REST API 的功能。例如,為了協助客戶了解您的 API,您可以提供 API 的文件。若要啟用此功能,請新增受支援 API 實體的 DocumentationPart 資源。
若要控制用戶端呼叫 API 的方式,請使用 IAM 許可、Lambda 授權方或 Amazon Cognito 使用者集區。若要測量 API 的用量,請設定 usage plans (用量方案) 來調節 API 請求。您可以在建立或更新 API 時啟用這些功能。
下圖顯示可用於 REST API 開發的功能,以及請求/回應模型中可設定這些功能的位置。
如需有關如何建立 API 的簡介,請參閱 教學:建立具 Lambda 代理整合的 REST API。若要進一步了解開發 REST API 時可能會使用的 API Gateway 功能,請參閱下列主題。這些主題包含您可以使用 API Gateway 主控台、API Gateway REST API、AWS CLI 或其中一個 AWS SDK 執行的概念式資訊和程序。