

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

# 在 API Gateway 中設定方法回應
<a name="api-gateway-method-settings-method-response"></a>

API 方法回應可封裝用戶端將會接收的 API 方法請求輸出。該輸出資料包含 HTTP 狀態碼、一些標頭，並可能包含本文。

透過非代理整合，指定的回應參數與本文可從相關聯的整合回應資料映射，或根據映射指派特定靜態值。這些映射是在整合回應中指定。此映射可以是依現狀傳遞整合回應的相同轉換。

透過代理整合，API Gateway 會自動將後端回應傳遞至方法回應。您不需要設定 API 方法回應。不過，若使用 Lambda 代理整合，Lambda 函數必須傳回[此輸出格式](set-up-lambda-proxy-integrations.md#api-gateway-simple-proxy-for-lambda-output-format)的結果，API Gateway 才能成功將整合回應映射至方法回應。

就程式設計而言，此方法回應設定相當於建立 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"}'
```