

# 开发 API Gateway 中的 REST API
<a name="rest-api-develop"></a>

 在 Amazon API Gateway 中，您可以将 REST API 构建为称为 API Gateway [资源](https://docs.amazonaws.cn/apigateway/latest/api/API_Resource.html)的可编程实体的集合。例如，您可以使用 [RestApi](https://docs.amazonaws.cn/apigateway/latest/api/API_RestApi.html) 资源表示可以包含[资源](https://docs.amazonaws.cn/apigateway/latest/api/API_Resource.html)实体集合的 API。

每个 `Resource` 实体可以具有一个或多个 [Method](https://docs.amazonaws.cn/apigateway/latest/api/API_Method.html) 资源。`Method` 是客户端提交的传入请求，可以包含以下请求参数：路径参数、标头或查询字符串参数。此外，根据 HTTP 方法，请求可以包含正文。您的方法定义了客户端如何访问公开的 `Resource`。要将 `Method` 与后端端点（也称为集成端点）进行集成，可创建 [Integration](https://docs.amazonaws.cn/apigateway/latest/api/API_Integration.html) 资源。这会将传入的请求转发到指定的集成端点 URI。如有必要，您可以转换请求参数或请求正文以满足后端要求，也可以创建代理集成，其中，API Gateway 将整个请求以标准化格式发送到集成端点 URI，然后直接将响应发送到客户端。

对于响应，您可以创建 [MethodResponse](https://docs.amazonaws.cn/apigateway/latest/api/API_MethodResponse.html) 资源来表示客户端收到的响应，然后创建 [IntegrationResponse](https://docs.amazonaws.cn/apigateway/latest/api/API_IntegrationResponse.html) 资源来表示后端返回的响应。使用集成响应来转换后端响应数据，然后将数据返回给客户端，或者按原样将后端响应传递给客户端。

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

下图显示了 API Gateway 如何为 HTTP 代理和 `GET /pets` 资源的 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 并在 Amazon Web Services 管理控制台中对其进行测试，请参阅[示例 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.amazonaws.cn/apigateway/latest/api/API_DocumentationPart.html) 资源。

要控制客户端调用 API 的方式，请使用 [IAM 权限](permissions.md)、[Lambda 授权方](apigateway-use-lambda-authorizer.md)或 [Amazon Cognito 用户池](apigateway-integrate-with-cognito.md)。要计量 API 的使用情况，请设置[使用计划](api-gateway-api-usage-plans.md)以限制 API 请求。您可在创建或更新 API 时启用这些选项。

下图显示了可用于 REST API 开发的功能，以及在请求/响应模型中配置这些功能的位置。

有关如何创建 API 的介绍，请参阅[教程：利用 Lambda 代理集成创建 REST API](api-gateway-create-api-as-simple-proxy-for-lambda.md)。要了解有关开发 REST API 时可能使用的 API Gateway 功能的更多信息，请参阅以下主题。这些主题包含概念信息以及您可以使用 API Gateway 控制台、API Gateway REST API、Amazon CLI 或其中一个 Amazon SDK 执行的过程。

**Topics**
+ [REST API 的示例资源](#rest-api-develop-example)
+ [用于开发的其它 REST API 功能](#rest-api-develop-details)
+ [API Gateway 中针对 REST API 的 API 端点类型](api-gateway-api-endpoint-types.md)
+ [API Gateway 中的 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)