

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

# 設定 OpenAPI 中的閘道回應自訂
<a name="set-up-gateway-responses-in-swagger"></a>

 您可以在 API 根層級使用 `x-amazon-apigateway-gateway-responses` 延伸，來自訂 OpenAPI 中的閘道回應。下列 OpenAPI 定義示範如何自訂 `MISSING_AUTHENTICATION_TOKEN` 類型的 [GatewayResponse](https://docs.aws.amazon.com/apigateway/latest/api/API_GatewayResponse.html)。

```
  "x-amazon-apigateway-gateway-responses": {
    "MISSING_AUTHENTICATION_TOKEN": {
      "statusCode": 404,
      "responseParameters": {
        "gatewayresponse.header.x-request-path": "method.input.params.petId",
        "gatewayresponse.header.x-request-query": "method.input.params.q",
        "gatewayresponse.header.Access-Control-Allow-Origin": "'a.b.c'",
        "gatewayresponse.header.x-request-header": "method.input.params.Accept"
      },
      "responseTemplates": {
        "application/json": "{\n     \"message\": $context.error.messageString,\n     \"type\":  \"$context.error.responseType\",\n     \"stage\":  \"$context.stage\",\n     \"resourcePath\":  \"$context.resourcePath\",\n     \"stageVariables.a\":  \"$stageVariables.a\",\n     \"statusCode\": \"'404'\"\n}"
      }
    }
```

在此範例中，自訂會將狀態碼從預設值 (`403`) 變更為 `404`。它也會將 `application/json` 媒體類型的四個標頭參數與一個內文對應範本新增至閘道回應。