如何解决来自 API Gateway 的“Invalid mapping expression specified”错误?

2 分钟阅读
0

我使用 AWS CloudFormation 模板、OpenAPI 定义或 AWS 命令行界面(AWS CLI)命令,使用代理资源创建了 Amazon API Gateway API。当我尝试向 API 添加 URL 路径参数时,我收到“Invalid mapping expression specified”错误消息。

简短描述

当代理路径参数 {proxy+} 没有定义的 URL 路径参数映射时,API Gateway 会返回“Invalid mapping expression specified”错误消息。

要解决此问题,请完成用于创建 API 的工具的步骤。

解决方法

CloudFormation 模板

定义 AWS::ApiGateway::Method 部分的 RequestParameters 属性。

完成以下步骤:

  1. 更新 CloudFormation 模板,将 RequestParameters 值设置为 true

    示例模板:

    ....
    .
      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. 要更新您的 API,请使用 CloudFormation 模板更新 CloudFormation 堆栈

**注意:**有关如何更新 API Gatew 资源的更多信息,请参阅 Amazon API Gateway resource type reference

OpenAPI 定义

x-amazon-apigateway-any-method 对象 中定义参数部分。

完成以下步骤:

  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。

**注意:**有关更多信息,请参阅 Swagger 网站上的 Describing parameters

AWS CLI 命令

**注意:**如果在运行 AWS CLI 命令时收到错误,请参阅 Troubleshoot AWS CLI errors。此外,确保您使用的是最新版本的 AWS CLI

当您运行 put-integration 命令来设置集成时,添加**--request-parameters**。

  1. 运行 update-method 命令以更新 HTTP 方法的方法请求配置:

    aws apigateway update-method \
        --rest-api-id <your-api-id> \
        --resource-id <your-resource-id> \
        --http-method <your-http-method> \
        --patch-operations op="add",path="/requestParameters/method.request.path.proxy",value="true"
  2. 运行 put-integration 命令,更新具有**--request-parameters** 的 HTTP 方法的集成请求配置。示例参数名为 proxy,其值为 method.request.path.proxyput-integration 命令设置与 VPC 链接HTTP_PROXY 集成:

    aws apigateway put-integration \
        --rest-api-id <your-api-id>\
        --resource-id <your-resource-id> \
        --http-method <your-http-method> \
        --type HTTP_PROXY \
        --integration-http-method <your-integration-http-method> \
        --uri "<your-integration-endpoint-uri>" \
        --connection-type VPC_LINK \
        --connection-id <your-vpclink-connection-id> \
        --request-parameters "integration.request.path.proxy=method.request.path.proxy"

    **注意:**请将所有示例值替换为您的值。

测试设置

完成以下步骤:

  1. 打开 API Gateway 控制台,然后选择您的 API 的名称。
  2. 添加 URL 路径参数
    **注意:**如果您的代理路径参数包含正确定义的 URL 路径参数映射,则不会出现错误。

相关信息

x-amazon-apigateway-integration.requestParameters 对象

设置具有代理资源的代理集成

针对 API Gateway 中 REST API 的数据转换

AWS 官方
AWS 官方已更新 1 年前