選取您的 Cookie 偏好設定

我們使用提供自身網站和服務所需的基本 Cookie 和類似工具。我們使用效能 Cookie 收集匿名統計資料,以便了解客戶如何使用我們的網站並進行改進。基本 Cookie 無法停用,但可以按一下「自訂」或「拒絕」以拒絕效能 Cookie。

如果您同意,AWS 與經核准的第三方也會使用 Cookie 提供實用的網站功能、記住您的偏好設定,並顯示相關內容,包括相關廣告。若要接受或拒絕所有非必要 Cookie,請按一下「接受」或「拒絕」。若要進行更詳細的選擇,請按一下「自訂」。

API Gateway 中 REST APIs參數映射範例

焦點模式
API Gateway 中 REST APIs參數映射範例 - Amazon API Gateway

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

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

下列範例示範如何使用 API Gateway 主控台、OpenAPI 和 AWS CloudFormation 範本建立參數映射表達式。如需如何使用參數映射來建立所需 CORS 標頭的範例,請參閱 API Gateway 中 REST API 的 CORS

範例 1:將方法請求參數映射至整合請求參數

下列範例會將方法請求標頭參數 映射puppies至整合請求標頭參數 DogsAge0

AWS Management Console
映射方法請求參數
  1. 在以下網址登入 API Gateway 主控台:https://console.aws.amazon.com/apigateway

  2. 選擇 REST API。

  3. 選擇方法。

    您的方法必須具有非代理整合。

  4. 針對方法請求設定,選擇編輯

  5. 選擇 HTTP 請求標頭

  6. 選擇新增標頭

  7. 對於名稱,輸入 puppies

  8. 選擇 Save (儲存)。

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

    AWS Management Console 會自動puppies為您從 新增參數映射method.request.header.puppies 至 ,但您需要變更名稱以符合整合端點預期的請求標頭參數。

  10. 對於名稱,輸入 DogsAge0

  11. 選擇 Save (儲存)。

  12. 重新部署 API 以使變更生效。

下列步驟說明如何驗證參數映射是否成功。

(選用) 測試參數映射
  1. 選擇測試標籤。您可能需要選擇向右箭頭按鈕才能顯示此索引標籤。

  2. 針對標頭,輸入 puppies:true

  3. 選擇測試

  4. 日誌中,結果應如下所示:

    Tue Feb 04 00:28:36 UTC 2025 : Method request headers: {puppies=true} Tue Feb 04 00:28:36 UTC 2025 : Method request body before transformations: Tue Feb 04 00:28:36 UTC 2025 : Endpoint request URI: http://petstore-demo-endpoint.execute-api.com/petstore/pets Tue Feb 04 00:28:36 UTC 2025 : Endpoint request headers: {DogsAge0=true, x-amzn-apigateway-api-id=abcd1234, Accept=application/json, User-Agent=AmazonAPIGateway_aaaaaaa, X-Amzn-Trace-Id=Root=1-abcd-12344}

    請求標頭參數已從 變更為 puppies DogsAge0

AWS CloudFormation

在此範例中,您使用內文屬性將 OpenAPI 定義檔案匯入 API Gateway。

AWSTemplateFormatVersion: 2010-09-09 Resources: Api: Type: 'AWS::ApiGateway::RestApi' Properties: Body: openapi: 3.0.1 info: title: ParameterMappingExample version: "2025-02-04T00:30:41Z" paths: /pets: get: parameters: - name: puppies in: header schema: type: string responses: "200": description: 200 response x-amazon-apigateway-integration: httpMethod: GET uri: http://petstore-demo-endpoint.execute-api.com/petstore/pets responses: default: statusCode: "200" requestParameters: integration.request.header.DogsAge0: method.request.header.puppies passthroughBehavior: when_no_match type: http ApiGatewayDeployment: Type: 'AWS::ApiGateway::Deployment' DependsOn: Api Properties: RestApiId: !Ref Api ApiGatewayDeployment20250219: Type: 'AWS::ApiGateway::Deployment' DependsOn: Api Properties: RestApiId: !Ref Api Stage: Type: 'AWS::ApiGateway::Stage' Properties: DeploymentId: !Ref ApiGatewayDeployment20250219 RestApiId: !Ref Api StageName: prod
OpenAPI
{ "openapi" : "3.0.1", "info" : { "title" : "ParameterMappingExample", "version" : "2025-02-04T00:30:41Z" }, "paths" : { "/pets" : { "get" : { "parameters" : [ { "name" : "puppies", "in" : "header", "schema" : { "type" : "string" } } ], "responses" : { "200" : { "description" : "200 response" } }, "x-amazon-apigateway-integration" : { "httpMethod" : "GET", "uri" : "http://petstore-demo-endpoint.execute-api.com/petstore/pets", "responses" : { "default" : { "statusCode" : "200" } }, "requestParameters" : { "integration.request.header.DogsAge0" : "method.request.header.puppies" }, "passthroughBehavior" : "when_no_match", "type" : "http" } } } } }
映射方法請求參數
  1. 在以下網址登入 API Gateway 主控台:https://console.aws.amazon.com/apigateway

  2. 選擇 REST API。

  3. 選擇方法。

    您的方法必須具有非代理整合。

  4. 針對方法請求設定,選擇編輯

  5. 選擇 HTTP 請求標頭

  6. 選擇新增標頭

  7. 對於名稱,輸入 puppies

  8. 選擇 Save (儲存)。

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

    AWS Management Console 會自動puppies為您從 新增參數映射method.request.header.puppies 至 ,但您需要變更名稱以符合整合端點預期的請求標頭參數。

  10. 對於名稱,輸入 DogsAge0

  11. 選擇 Save (儲存)。

  12. 重新部署 API 以使變更生效。

下列步驟說明如何驗證參數映射是否成功。

(選用) 測試參數映射
  1. 選擇測試標籤。您可能需要選擇向右箭頭按鈕才能顯示此索引標籤。

  2. 針對標頭,輸入 puppies:true

  3. 選擇測試

  4. 日誌中,結果應如下所示:

    Tue Feb 04 00:28:36 UTC 2025 : Method request headers: {puppies=true} Tue Feb 04 00:28:36 UTC 2025 : Method request body before transformations: Tue Feb 04 00:28:36 UTC 2025 : Endpoint request URI: http://petstore-demo-endpoint.execute-api.com/petstore/pets Tue Feb 04 00:28:36 UTC 2025 : Endpoint request headers: {DogsAge0=true, x-amzn-apigateway-api-id=abcd1234, Accept=application/json, User-Agent=AmazonAPIGateway_aaaaaaa, X-Amzn-Trace-Id=Root=1-abcd-12344}

    請求標頭參數已從 變更為 puppies DogsAge0

範例 2:將多個方法請求參數映射至不同的整合請求參數

下列範例會將多值方法請求查詢字串參數映射methodRequestQueryParam至整合請求查詢字串參數,integrationQueryParam並將方法請求標頭參數映射methodRequestHeaderParam至整合請求路徑參數 integrationPathParam

AWS Management Console
映射方法請求參數
  1. 在以下網址登入 API Gateway 主控台:https://console.aws.amazon.com/apigateway

  2. 選擇 REST API。

  3. 選擇方法。

    您的方法必須具有非代理整合。

  4. 針對方法請求設定,選擇編輯

  5. 選擇 URL 查詢字串參數

  6. 選擇新增查詢字串

  7. 對於名稱,輸入 methodRequestQueryParam

  8. 選擇 HTTP 請求標頭

  9. 選擇新增標頭

  10. 對於名稱,輸入 methodRequestHeaderParam

  11. 選擇 Save (儲存)。

  12. 選擇整合請求索引標籤,然後針對整合請求設定,選擇編輯

  13. 選擇 URL 路徑參數

  14. 選擇新增路徑參數

  15. 對於名稱,輸入 integrationPathParam

  16. 對於映射自,輸入 method.request.header.methodRequestHeaderParam

    這會將您在方法請求中指定的方法請求標頭映射至新的整合請求路徑參數。

  17. 選擇 URL 查詢字串參數

  18. 選擇新增查詢字串

  19. 對於名稱,輸入 integrationQueryParam

  20. 對於映射自,輸入 method.request.multivaluequerystring.methodRequestQueryParam

    這會將多值查詢字串參數映射到新的單一值整合請求查詢字串參數。

  21. 選擇 Save (儲存)。

  22. 重新部署 API 以使變更生效。

AWS CloudFormation

在此範例中,您使用內文屬性將 OpenAPI 定義檔案匯入 API Gateway。

下列 OpenAPI 定義會為 HTTP 整合建立下列參數映射:

  • 整合請求路徑參數methodRequestHeaderParam中名為 的方法請求標頭 integrationPathParam

  • 在整合請求查詢字串methodRequestQueryParam中名為 的多值方法請求查詢字串 integrationQueryParam

AWSTemplateFormatVersion: 2010-09-09 Resources: Api: Type: 'AWS::ApiGateway::RestApi' Properties: Body: openapi: 3.0.1 info: title: Parameter mapping example 2 version: "2025-01-15T19:12:31Z" paths: /: post: parameters: - name: methodRequestQueryParam in: query schema: type: string - name: methodRequestHeaderParam in: header schema: type: string responses: "200": description: 200 response x-amazon-apigateway-integration: httpMethod: GET uri: http://petstore-demo-endpoint.execute-api.com/petstore/pets responses: default: statusCode: "200" requestParameters: integration.request.querystring.integrationQueryParam: method.request.multivaluequerystring.methodRequestQueryParam integration.request.path.integrationPathParam: method.request.header.methodRequestHeaderParam requestTemplates: application/json: '{"statusCode": 200}' passthroughBehavior: when_no_templates timeoutInMillis: 29000 type: http ApiGatewayDeployment: Type: 'AWS::ApiGateway::Deployment' DependsOn: Api Properties: RestApiId: !Ref Api ApiGatewayDeployment20250219: Type: 'AWS::ApiGateway::Deployment' DependsOn: Api Properties: RestApiId: !Ref Api Stage: Type: 'AWS::ApiGateway::Stage' Properties: DeploymentId: !Ref ApiGatewayDeployment20250219 RestApiId: !Ref Api StageName: prod
OpenAPI

下列 OpenAPI 定義會為 HTTP 整合建立下列參數映射:

  • 整合請求路徑參數methodRequestHeaderParam中名為 的方法請求標頭 integrationPathParam

  • 在整合請求查詢字串methodRequestQueryParam中名為 的多值方法請求查詢字串 integrationQueryParam

{ "openapi" : "3.0.1", "info" : { "title" : "Parameter mapping example 2", "version" : "2025-01-15T19:12:31Z" }, "paths" : { "/" : { "post" : { "parameters" : [ { "name" : "methodRequestQueryParam", "in" : "query", "schema" : { "type" : "string" } }, { "name" : "methodRequestHeaderParam", "in" : "header", "schema" : { "type" : "string" } } ], "responses" : { "200" : { "description" : "200 response" } }, "x-amazon-apigateway-integration" : { "httpMethod" : "GET", "uri" : "http://petstore-demo-endpoint.execute-api.com/petstore/pets", "responses" : { "default" : { "statusCode" : "200" } }, "requestParameters" : { "integration.request.querystring.integrationQueryParam" : "method.request.multivaluequerystring.methodRequestQueryParam", "integration.request.path.integrationPathParam" : "method.request.header.methodRequestHeaderParam" }, "requestTemplates" : { "application/json" : "{\"statusCode\": 200}" }, "passthroughBehavior" : "when_no_templates", "timeoutInMillis" : 29000, "type" : "http" } } } } }
映射方法請求參數
  1. 在以下網址登入 API Gateway 主控台:https://console.aws.amazon.com/apigateway

  2. 選擇 REST API。

  3. 選擇方法。

    您的方法必須具有非代理整合。

  4. 針對方法請求設定,選擇編輯

  5. 選擇 URL 查詢字串參數

  6. 選擇新增查詢字串

  7. 對於名稱,輸入 methodRequestQueryParam

  8. 選擇 HTTP 請求標頭

  9. 選擇新增標頭

  10. 對於名稱,輸入 methodRequestHeaderParam

  11. 選擇 Save (儲存)。

  12. 選擇整合請求索引標籤,然後針對整合請求設定,選擇編輯

  13. 選擇 URL 路徑參數

  14. 選擇新增路徑參數

  15. 對於名稱,輸入 integrationPathParam

  16. 對於映射自,輸入 method.request.header.methodRequestHeaderParam

    這會將您在方法請求中指定的方法請求標頭映射至新的整合請求路徑參數。

  17. 選擇 URL 查詢字串參數

  18. 選擇新增查詢字串

  19. 對於名稱,輸入 integrationQueryParam

  20. 對於映射自,輸入 method.request.multivaluequerystring.methodRequestQueryParam

    這會將多值查詢字串參數映射到新的單一值整合請求查詢字串參數。

  21. 選擇 Save (儲存)。

  22. 重新部署 API 以使變更生效。

範例 3:將欄位從 JSON 請求內文映射至整合請求參數

您也可以使用 JSONPath 表達式,從 JSON 請求內文中的欄位映射整合請求參數。下列範例會將方法請求內文映射至名為 的整合請求標頭,body-header並將部分請求內文映射至名為 的整合請求標頭,如 JSON 表達式所示pet-price

若要測試此範例,請提供包含價格類別的輸入,如下所示:

[ { "id": 1, "type": "dog", "price": 249.99 } ]
AWS Management Console
映射方法請求參數
  1. 在以下網址登入 API Gateway 主控台:https://console.aws.amazon.com/apigateway

  2. 選擇 REST API。

  3. 選擇 POSTPATCHPUTANY方法。

    您的方法必須具有非代理整合。

  4. 針對整合請求設定,選擇編輯

  5. 選擇 URL 請求標頭參數

  6. 選擇新增請求標頭參數

  7. 對於名稱,輸入 body-header

  8. 對於映射自,輸入 method.request.body

    這會將方法請求內文映射至新的整合請求標頭參數。

  9. 選擇新增請求標頭參數

  10. 對於名稱,輸入 pet-price

  11. 對於映射自,輸入 method.request.body[0].price

    這會將方法請求內文的一部分映射至新的整合請求標頭參數。

  12. 選擇 Save (儲存)。

  13. 重新部署 API 以使變更生效。

AWS CloudFormation

在此範例中,您使用內文屬性將 OpenAPI 定義檔案匯入 API Gateway。

AWSTemplateFormatVersion: 2010-09-09 Resources: Api: Type: 'AWS::ApiGateway::RestApi' Properties: Body: openapi: 3.0.1 info: title: Parameter mapping example 3 version: "2025-01-15T19:19:14Z" paths: /: post: responses: "200": description: 200 response x-amazon-apigateway-integration: httpMethod: GET uri: http://petstore-demo-endpoint.execute-api.com/petstore/pets responses: default: statusCode: "200" requestParameters: integration.request.header.pet-price: method.request.body[0].price integration.request.header.body-header: method.request.body requestTemplates: application/json: '{"statusCode": 200}' passthroughBehavior: when_no_templates timeoutInMillis: 29000 type: http ApiGatewayDeployment: Type: 'AWS::ApiGateway::Deployment' DependsOn: Api Properties: RestApiId: !Ref Api ApiGatewayDeployment20250219: Type: 'AWS::ApiGateway::Deployment' DependsOn: Api Properties: RestApiId: !Ref Api Stage: Type: 'AWS::ApiGateway::Stage' Properties: DeploymentId: !Ref ApiGatewayDeployment20250219 RestApiId: !Ref Api StageName: prod
OpenAPI

下列 OpenAPI 定義會從 JSON 請求內文中的欄位映射整合請求參數。

{ "openapi" : "3.0.1", "info" : { "title" : "Parameter mapping example 3", "version" : "2025-01-15T19:19:14Z" }, "paths" : { "/" : { "post" : { "responses" : { "200" : { "description" : "200 response" } }, "x-amazon-apigateway-integration" : { "httpMethod" : "GET", "uri" : "http://petstore-demo-endpoint.execute-api.com/petstore/pets", "responses" : { "default" : { "statusCode" : "200" } }, "requestParameters" : { "integration.request.header.pet-price" : "method.request.body[0].price", "integration.request.header.body-header" : "method.request.body" }, "requestTemplates" : { "application/json" : "{\"statusCode\": 200}" }, "passthroughBehavior" : "when_no_templates", "timeoutInMillis" : 29000, "type" : "http" } } } } }
映射方法請求參數
  1. 在以下網址登入 API Gateway 主控台:https://console.aws.amazon.com/apigateway

  2. 選擇 REST API。

  3. 選擇 POSTPATCHPUTANY方法。

    您的方法必須具有非代理整合。

  4. 針對整合請求設定,選擇編輯

  5. 選擇 URL 請求標頭參數

  6. 選擇新增請求標頭參數

  7. 對於名稱,輸入 body-header

  8. 對於映射自,輸入 method.request.body

    這會將方法請求內文映射至新的整合請求標頭參數。

  9. 選擇新增請求標頭參數

  10. 對於名稱,輸入 pet-price

  11. 對於映射自,輸入 method.request.body[0].price

    這會將方法請求內文的一部分映射至新的整合請求標頭參數。

  12. 選擇 Save (儲存)。

  13. 重新部署 API 以使變更生效。

範例 4:將整合回應映射至方法回應

您也可以將整合回應映射至方法回應。下列範例會將整合回應內文映射至名為 的方法回應標頭location、將整合回應標頭映射x-app-id至方法回應標頭 id,並將多值整合回應標頭映射item至方法回應標頭 items

AWS Management Console
映射整合回應
  1. 在以下網址登入 API Gateway 主控台:https://console.aws.amazon.com/apigateway

  2. 選擇 REST API。

  3. 選擇方法。

    您的方法必須具有非代理整合。

  4. 選擇方法回應索引標籤,然後在回應 200 中選擇編輯

  5. 針對標頭名稱,選擇新增標頭

  6. 建立三個名為 iditem和 的標頭location

  7. 選擇 Save (儲存)。

  8. 選擇整合回應索引標籤,然後針對預設 - 回應,選擇編輯

  9. 標頭映射下,輸入以下內容。

    1. 針對 ID,輸入 integration.response.header.x-app-id

    2. 針對項目,輸入 integration.response.multivalueheader.item

    3. 針對位置,輸入 integration.response.body.redirect.url

  10. 選擇 Save (儲存)。

  11. 重新部署 API 以使變更生效。

AWS CloudFormation

在此範例中,您使用內文屬性將 OpenAPI 定義檔案匯入 API Gateway。

AWSTemplateFormatVersion: 2010-09-09 Resources: Api: Type: 'AWS::ApiGateway::RestApi' Properties: Body: openapi: 3.0.1 info: title: Parameter mapping example version: "2025-01-15T19:21:35Z" paths: /: post: responses: "200": description: 200 response headers: item: schema: type: string location: schema: type: string id: schema: type: string x-amazon-apigateway-integration: type: http httpMethod: GET uri: http://petstore-demo-endpoint.execute-api.com/petstore/pets responses: default: statusCode: "200" responseParameters: method.response.header.id: integration.response.header.x-app-id method.response.header.location: integration.response.body.redirect.url method.response.header.item: integration.response.multivalueheader.item requestTemplates: application/json: '{"statusCode": 200}' passthroughBehavior: when_no_templates timeoutInMillis: 29000 ApiGatewayDeployment: Type: 'AWS::ApiGateway::Deployment' DependsOn: Api Properties: RestApiId: !Ref Api ApiGatewayDeployment20250219: Type: 'AWS::ApiGateway::Deployment' DependsOn: Api Properties: RestApiId: !Ref Api Stage: Type: 'AWS::ApiGateway::Stage' Properties: DeploymentId: !Ref ApiGatewayDeployment20250219 RestApiId: !Ref Api StageName: prod
OpenAPI

下列 OpenAPI 定義會將整合回應映射至方法回應。

{ "openapi" : "3.0.1", "info" : { "title" : "Parameter mapping example", "version" : "2025-01-15T19:21:35Z" }, "paths" : { "/" : { "post" : { "responses" : { "200" : { "description" : "200 response", "headers" : { "item" : { "schema" : { "type" : "string" } }, "location" : { "schema" : { "type" : "string" } }, "id" : { "schema" : { "type" : "string" } } } } }, "x-amazon-apigateway-integration" : { "type" : "http", "httpMethod" : "GET", "uri" : "http://petstore-demo-endpoint.execute-api.com/petstore/pets", "responses" : { "default" : { "statusCode" : "200", "responseParameters" : { "method.response.header.id" : "integration.response.header.x-app-id", "method.response.header.location" : "integration.response.body.redirect.url", "method.response.header.item" : "integration.response.multivalueheader.item" } } }, "requestTemplates" : { "application/json" : "{\"statusCode\": 200}" }, "passthroughBehavior" : "when_no_templates", "timeoutInMillis" : 29000 } } } } }
映射整合回應
  1. 在以下網址登入 API Gateway 主控台:https://console.aws.amazon.com/apigateway

  2. 選擇 REST API。

  3. 選擇方法。

    您的方法必須具有非代理整合。

  4. 選擇方法回應索引標籤,然後在回應 200 中選擇編輯

  5. 針對標頭名稱,選擇新增標頭

  6. 建立三個名為 iditem和 的標頭location

  7. 選擇 Save (儲存)。

  8. 選擇整合回應索引標籤,然後針對預設 - 回應,選擇編輯

  9. 標頭映射下,輸入以下內容。

    1. 針對 ID,輸入 integration.response.header.x-app-id

    2. 針對項目,輸入 integration.response.multivalueheader.item

    3. 針對位置,輸入 integration.response.body.redirect.url

  10. 選擇 Save (儲存)。

  11. 重新部署 API 以使變更生效。

隱私權網站條款Cookie 偏好設定
© 2025, Amazon Web Services, Inc.或其附屬公司。保留所有權利。