覆蓋的請求和響應參數以及狀態碼的示例 API - Amazon API 网关

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

覆蓋的請求和響應參數以及狀態碼的示例 API

下列範例顯示如何建立和測試對應範本覆寫。前兩個例子使用 AWS Management Console 和示例API作為起點。最後兩個範例使用 AWS CLI 和SDK的 JavaScript。

使用覆寫API的回應狀態碼 AWS Management Console

若要使用 PetStore 範例擷取寵物API,請使用的API方法要求GET /pets/{petId},其中{petId}是可以在執行階段取得數字的 path 參數。

在此範例中,您將建立對應至偵測到錯誤條件400時對應的對應範本,$context.responseOverride.status以覆寫此GET方法的回應程式碼。

  1. 請在 https://console.aws.amazon.com/apigateway 登入API閘道主控台。

  2. 在下方 APIs,選擇 PetStore API,然後選擇 [資源]。

  3. 資源樹狀結構中的 /{petId} 下,選擇 GET 方法。

  4. 選擇測試標籤。您可能需要選擇向右箭頭按鈕才能顯示此索引標籤。

  5. 對於 petId,請輸入-1,然後選擇 [測試]。

    您會在結果中注意到兩件事:

    首先,響應主體指示一個 out-of-range 錯誤:

    { "errors": [ { "key": "GetPetRequest.petId", "message": "The value is out of range." } ] }

    其次,在日誌方塊下的最後一列結尾為:Method completed with status: 200

  6. 整合回應索引標籤上,針對預設 - 回應,選擇編輯

  7. 選擇對應範本

  8. 選擇新增對應範本

  9. 針對內容類型,輸入 application/json

  10. 針對範本內文,輸入下列內容:

    #set($inputRoot = $input.path('$')) $input.json("$") #if($inputRoot.toString().contains("error")) #set($context.responseOverride.status = 400) #end
  11. 選擇儲存

  12. 選擇測試標籤。

  13. 針對 petId,請輸入 -1

  14. 在結果中,響應主體指示錯 out-of-range 誤:

    { "errors": [ { "key": "GetPetRequest.petId", "message": "The value is out of range." } ] }

    然而,在 Logs (日誌) 方塊下的最後一列目前結尾為:Method completed with status: 400

使用覆寫API的要求參數和標頭 AWS Management Console

在此示例中,您將通過創建一個映射模板映射到組合另外兩個標頭的新標頭$context.requestOverride.header.header_name來覆蓋該GET方法的請求頭代碼。

  1. 請在 https://console.aws.amazon.com/apigateway 登入API閘道主控台。

  2. 在下 APIs,選擇 PetStore API.

  3. 資源樹狀結構中的 /pet 下,選擇 GET 方法。

  4. 方法請求索引標籤上,針對方法請求設定,選擇編輯

  5. 選擇HTTP要求標頭,然後選擇 [新增標頭]。

  6. 針對名稱,輸入 header1

  7. 選擇新增標頭,然後建立名為 header2 的第二個標頭。

  8. 選擇儲存

  9. 整合請求索引標籤上,針對整合請求設定,選擇編輯

  10. 針對請求內文傳遞,選取未定義範本時 (建議)

  11. 選擇對應範本,然後執行下列動作:

    1. 選擇新增對應範本

    2. 針對內容類型,輸入 application/json

    3. 針對範本內文,輸入下列內容:

      #set($header1Override = "foo") #set($header3Value = "$input.params('header1')$input.params('header2')") $input.json("$") #set($context.requestOverride.header.header3 = $header3Value) #set($context.requestOverride.header.header1 = $header1Override) #set($context.requestOverride.header.multivalueheader=[$header1Override, $header3Value])
  12. 選擇儲存

  13. 選擇測試標籤。

  14. {pets} ({pets})Headers (標頭) 下,複製下列程式碼:

    header1:header1Val header2:header2Val
  15. 選擇 Test (測試)

    在日誌中,您應該會看到一個包括此文字的項目:

    Endpoint request headers: {header3=header1Valheader2Val, header2=header2Val, header1=foo, x-amzn-apigateway-api-id=<api-id>, Accept=application/json, multivalueheader=foo,header1Valheader2Val}

使用覆寫API的要求參數和標頭 AWS CLI

下面的CLI例子演示了如何使用put-integration命令來覆蓋響應代碼:

aws apigateway put-integration --rest-api-id <API_ID> --resource-id <PATH_TO_RESOURCE_ID> --http-method <METHOD> --type <INTEGRATION_TYPE> --request-templates <REQUEST_TEMPLATE_MAP>

where <REQUEST_TEMPLATE_MAP> 是從內容類型到要套用之範本字串的對映。對應的結構如下所示:

Content_type1=template_string,Content_type2=template_string

或者,在JSON語法中:

{"content_type1": "template_string" ...}

下列範例會示範如何使用put-integration-response命令覆寫API的回應碼:

aws apigateway put-integration-response --rest-api-id <API_ID> --resource-id <PATH_TO_RESOURCE_ID> --http-method <METHOD> --status-code <STATUS_CODE> --response-templates <RESPONSE_TEMPLATE_MAP>

where <RESPONSE_TEMPLATE_MAP> 具有相同的格式 <REQUEST_TEMPLATE_MAP> 以上。

使用 API for 覆寫的要求參數和標頭 SDK JavaScript

以下範例示範如何使用 put-integration 命令來覆寫回應程式碼:

要求:

var params = { httpMethod: 'STRING_VALUE', /* required */ resourceId: 'STRING_VALUE', /* required */ restApiId: 'STRING_VALUE', /* required */ type: HTTP | AWS | MOCK | HTTP_PROXY | AWS_PROXY, /* required */ requestTemplates: { '<Content_type>': 'TEMPLATE_STRING', /* '<String>': ... */ }, }; apigateway.putIntegration(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response });

回應:

var params = { httpMethod: 'STRING_VALUE', /* required */ resourceId: 'STRING_VALUE', /* required */ restApiId: 'STRING_VALUE', /* required */ statusCode: 'STRING_VALUE', /* required */ responseTemplates: { '<Content_type>': 'TEMPLATE_STRING', /* '<String>': ... */ }, }; apigateway.putIntegrationResponse(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response });