

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在 APIs Gateway 中開發 REST API
<a name="rest-api-develop"></a>

 在 Amazon API Gateway 中，您可以將 REST API 建置為可程式化實體的集合，稱為 API Gateway [資源](https://docs.aws.amazon.com/apigateway/latest/api/API_Resource.html)。例如，您可以使用 [RestApi](https://docs.aws.amazon.com/apigateway/latest/api/API_RestApi.html) 資源來代表可包含 [Resource](https://docs.aws.amazon.com/apigateway/latest/api/API_Resource.html) 實體集合的 API。

每個 `Resource` 實體可以擁有一或多個 [Method](https://docs.aws.amazon.com/apigateway/latest/api/API_Method.html) 資源。`Method` 是用戶端提交的傳入請求，可包含下列請求參數：路徑參數、標頭或查詢字串參數。此外，根據 HTTP 方法而定，請求可以包含內文。您的方法會定義用戶端存取公開 `Resource` 的方式。若要將 `Method` 與後端端點整合，也稱為整合端點，您可以建立[整合](https://docs.aws.amazon.com/apigateway/latest/api/API_Integration.html)資源。這會將傳入請求轉送到指定的整合端點 URI。如有必要，您可以轉換請求參數或請求內文以符合後端需求，也可以建立代理整合，其中 API Gateway 會以標準化格式將整個請求傳送至整合端點 URI，然後直接將回應傳送至用戶端。

針對回應，您可以建立 [MethodResponse](https://docs.aws.amazon.com/apigateway/latest/api/API_MethodResponse.html) 資源來代表用戶端接收的回應，並建立 [IntegrationResponse](https://docs.aws.amazon.com/apigateway/latest/api/API_IntegrationResponse.html) 資源來代表後端傳回的回應。請先使用整合回應來轉換後端回應資料，再將資料傳回至用戶端，或將後端回應依現狀傳遞到用戶端。

## REST API 的範例資源
<a name="rest-api-develop-example"></a>

下圖顯示 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 ]

![API Gateway 非代理整合圖](http://docs.aws.amazon.com/zh_tw/apigateway/latest/developerguide/images/develop-non-proxy.png)


------
#### [ Proxy integration ]

![API Gateway 代理整合圖](http://docs.aws.amazon.com/zh_tw/apigateway/latest/developerguide/images/develop-proxy.png)


------

下列範例執行日誌顯示 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 管理主控台，請參閱[範例 API](api-gateway-create-api-from-example.md)。

## 用於開發的其他 REST API 功能
<a name="rest-api-develop-details"></a>

API Gateway 支援其他用於開發 REST API 的功能。例如，為了協助客戶了解您的 API，您可以提供 API 的文件。若要啟用此功能，請新增受支援 API 實體的 [DocumentationPart](https://docs.aws.amazon.com/apigateway/latest/api/API_DocumentationPart.html) 資源。

若要控制用戶端呼叫 API 的方式，請使用 [IAM 許可](permissions.md)、[Lambda 授權方](apigateway-use-lambda-authorizer.md)或 [Amazon Cognito 使用者集區](apigateway-integrate-with-cognito.md)。若要測量 API 的用量，請設定 [ usage plans (用量方案)](api-gateway-api-usage-plans.md) 來調節 API 請求。您可以在建立或更新 API 時啟用這些功能。

下圖顯示可用於 REST API 開發的功能，以及請求/回應模型中可設定這些功能的位置。

![API Gateway 功能圖](http://docs.aws.amazon.com/zh_tw/apigateway/latest/developerguide/images/develop-features.png)


如需有關如何建立 API 的簡介，請參閱 [教學：建立具 Lambda 代理整合的 REST API](api-gateway-create-api-as-simple-proxy-for-lambda.md)。若要進一步了解開發 REST API 時可能會使用的 API Gateway 功能，請參閱下列主題。這些主題包含您可以使用 API Gateway 主控台、API Gateway REST API AWS CLI、 或其中一個 AWS SDKs 執行的概念資訊和程序。

**Topics**
+ [REST API 的範例資源](#rest-api-develop-example)
+ [用於開發的其他 REST API 功能](#rest-api-develop-details)
+ [API Gateway 中 REST API 的 API 端點類型](api-gateway-api-endpoint-types.md)
+ [APIs中 REST API 的安全政策](apigateway-security-policies.md)
+ [API Gateway 中 REST API 的 IP 位址類型](api-gateway-ip-address-type.md)
+ [API Gateway 中 REST API 的方法](how-to-method-settings.md)
+ [在 API Gateway 中控制和管理對 REST API 的存取](apigateway-control-access-to-api.md)
+ [API Gateway 中 REST API 的整合](how-to-integration-settings.md)
+ [API Gateway 中的 REST API 的請求驗證](api-gateway-method-request-validation.md)
+ [API Gateway 中用於 REST API 的資料轉換](rest-api-data-transformations.md)
+ [API Gateway 中 REST API 的閘道回應](api-gateway-gatewayResponse-definition.md)
+ [API Gateway 中 REST API 的 CORS](how-to-cors.md)
+ [API Gateway 中 REST API 的二進位媒體類型](api-gateway-payload-encodings.md)
+ [在 API Gateway 中調用 REST API](how-to-call-api.md)
+ [在 API Gateway 中使用 OpenAPI 開發 REST API](api-gateway-import-api.md)