我使用 AWS CloudFormation 範本 (或 OpenAPI 定義) 來建立具有 Proxy 資源的 Amazon API Gateway API。我嘗試將網址路徑參數新增至 API 時,系統出現「指定的對應運算式無效」錯誤訊息。如何解決此錯誤?
簡短說明
若 Proxy 路徑參數 {proxy+} 不具有已定義的網址路徑參數對應時,API Gateway 會傳回指定的對應運算式無效錯誤。
若要解決此錯誤,請執行下列操作:
解決方法
AWS CloudFormation 範本
1. 更新 CloudFormation 範本,以將 RequestParameters 值設定為 true。
範例 CloudFormation 範本 RequestParameters
...
.
.
ProxyMethod:
Type: 'AWS::ApiGateway::Method'
Properties:
.
.
RequestParameters:
method.request.path.proxy: true
Integration:
RequestParameters:
integration.request.path.proxy: 'method.request.path.proxy'
IntegrationHttpMethod: ANY
.
.
...
2. 使用 CloudFormation 範本更新 API,以更新 AWS CloudFormation 堆疊。
**注意:**如需有關如何更新 API Gateway 資源的詳細資訊,請參閱 Amazon API Gateway 資源類型參考。
OpenAPI 定義
1. 更新您的 API 定義,使 x-amazon-apigateway-any-method 區段下方的參數具有下列值:
"x-amazon-apigateway-any-method": {
"parameters": [
{
"name": "proxy",
"in": "path",
"required": true,
"type": "string"
}
]
....
....
}
2. 透過將更新的 API 定義檔匯入 API Gateway 以更新 API。
**注意:**如需詳細資訊,請參閱 OpenAPI 網站的說明參數。
測試設定
1. 在 API Gateway 主控台中,選擇 API 的名稱。
2. 依照您使用案例的需求,新增網址路徑參數。如果您的 Proxy 路徑參數包含正確定義的網址路徑參數對應,系統就不會出現錯誤。
相關資訊
x-amazon-apigateway-integration.requestParameters 物件
使用 Proxy 資源設定 Proxy 整合
設定 REST API 的資料轉換