

# 在 API Gateway 中设置方法响应
<a name="api-gateway-method-settings-method-response"></a>

API 方法响应将客户端将收到 API 方法请求的输出进行封装。输出数据包括 HTTP 状态代码、部分标头，还可能包含正文。

使用非代理集成，可从相关的集成响应数据映射指定的响应参数和正文，也可以根据映射分配特定静态值。集成响应中已指定这些映射。映射可以是按原样传递集成响应的恒等变换。

使用代理集成，API Gateway 可自动将后端响应传递到方法响应。您无需设置 API 方法响应。但如果使用 Lambda 代理集成，Lambda 函数必须为 API Gateway 返回[此输出格式](set-up-lambda-proxy-integrations.md#api-gateway-simple-proxy-for-lambda-output-format)的结果，才能将集成响应成功映射到方法响应。

通过编程，该方法响应设置相当于创建 API Gateway 的 [MethodResponse](https://docs.aws.amazon.com/apigateway/latest/api/API_MethodResponse.html) 资源和设置 [statusCode](https://docs.aws.amazon.com/apigateway/latest/api/API_MethodResponse.html#statusCode)、[responseParameters](https://docs.aws.amazon.com/apigateway/latest/api/API_MethodResponse.html#responseParameters) 和 [responseModels](https://docs.aws.amazon.com/apigateway/latest/api/API_MethodResponse.html#responseModels) 的属性。

为 API 方法设置状态代码时，您应该选择一个状态代码作为处理任何意料之外的集成响应的默认代码。将 `500` 设置默认状态代码比较合理，因为这相对于将未映射的响应转换为服务器端错误。出于说明原因，API Gateway 控制台将 `200` 响应设置为默认响应。但您可以将其重置为 `500` 响应。

要设置方法响应，您必须已创建方法请求。

## 设置方法响应状态代码
<a name="setup-method-response-status-code"></a>

方法响应的状态代码定义了响应类型。例如，响应 200、400 和 500 分别表示响应成功、客户端错误和服务器端错误。

要设置方法响应状态代码，请将 [https://docs.aws.amazon.com/apigateway/latest/api/API_MethodResponse.html#statusCode](https://docs.aws.amazon.com/apigateway/latest/api/API_MethodResponse.html#statusCode) 属性设置为 HTTP 状态代码。以下 [put-method-response](https://docs.aws.amazon.com/cli/latest/reference/apigateway/put-method-response.html) 命令创建 `200` 方法响应。

```
aws apigateway put-method-response \
    --rest-api-id vaz7da96z6 \ 
    --resource-id 6sxz2j \
    --http-method GET \
    --status-code 200
```

## 设置方法响应参数
<a name="setup-method-response-parameters"></a>

方法响应参数定义客户端在哪些标头中收到相关方法请求的响应。响应参数还会根据 API 方法的集成响应中规定的映射指定 API Gateway 将集成响应参数映射到的目标。

要设置方法请求参数，请添加到 `responseParameters` 格式的 `MethodResponse` 键/值对的 [https://docs.aws.amazon.com/apigateway/latest/api/API_MethodResponse.html#responseParameters](https://docs.aws.amazon.com/apigateway/latest/api/API_MethodResponse.html#responseParameters) 映射。以下 [put-method-response](https://docs.aws.amazon.com/cli/latest/reference/apigateway/put-method-response.html) 命令设置 `my-header` 标头。

```
aws apigateway put-method-response \
        --rest-api-id vaz7da96z6 \
        --resource-id 6sxz2j \
        --http-method GET \
        --status-code 200  \
        --response-parameters method.response.header.my-header=false
```

## 设置方法响应模型
<a name="setup-method-response-models"></a>

 

 方法响应模型定义了方法响应正文的格式。为 API 生成强类型开发工具包时，必须设置方法响应模型。它可确保输出将在 Java 或 Objective-C 中转换为适当的类。在其他情况下，设置模型是可选操作。

设置响应模型之前，必须先在 API Gateway 中创建模型。为此，可以调用 `[create-model](https://docs.aws.amazon.com/cli/latest/reference/apigateway/create-model.html)` 命令。以下 [create-model](https://docs.aws.amazon.com/cli/latest/reference/apigateway/create-model.html) 命令创建 `PetStorePet` 模型，用于描述对 `GET /pets/{petId}` 方法请求的响应正文。

```
aws apigateway create-model \
    --rest-api-id vaz7da96z6 \
    --content-type application/json \
    --name PetStorePet \
    --schema '{ \
                  "$schema": "http://json-schema.org/draft-04/schema#", \
                  "title": "PetStorePet", \
                  "type": "object", \
                  "properties": { \
                    "id": { "type": "number" }, \
                    "type": { "type": "string" }, \
                    "price": { "type": "number" } \
                  } \
              }'
```

结果将创建为 API Gateway [https://docs.aws.amazon.com/apigateway/latest/api/API_Model.html](https://docs.aws.amazon.com/apigateway/latest/api/API_Model.html) 资源。

要设置方法响应模型以定义有效负载格式，请将 "application/json":"PetStorePet" 密钥值对添加至 [https://docs.aws.amazon.com/apigateway/latest/api/API_MethodResponse.html](https://docs.aws.amazon.com/apigateway/latest/api/API_MethodResponse.html) 资源的 [https://docs.aws.amazon.com/apigateway/latest/api/API_MethodResponse.html#responseModels](https://docs.aws.amazon.com/apigateway/latest/api/API_MethodResponse.html#responseModels) 地图。以下 [put-method-response](https://docs.aws.amazon.com/cli/latest/reference/apigateway/put-method-response.html) 命令创建使用响应模型来定义有效载荷格式的方法响应：

```
aws apigateway put-method-response \
    --rest-api-id vaz7da96z6 \
    --resource-id 6sxz2j \
    --http-method GET \
    --status-code 200  \
    --response-parameters method.response.header.my-header=false \
    --response-models '{"application/json":"PetStorePet"}'
```