【问题标题】:requestParameters returning "Invalid mapping expression specified: true"requestParameters 返回“指定的映射表达式无效:true”
【发布时间】:2016-07-07 10:38:03
【问题描述】:

我正在配置 lambda 函数的 API 网关与 Serverless Framework 版本 0.4.2 的集成。

我的问题是定义端点的请求参数。 AWS docs for API gateway 条目说:

请求参数

表示 Amazon API Gateway 可以接受的请求参数。请求参数表示为键/值映射,源作为键,布尔标志作为值。布尔标志用于指定参数是否是必需的。源必须匹配模式 method.request.{location}.{name},其中 location 是查询字符串、路径或标头。 name 是一个有效的、唯一的参数名称。此处指定的源可用于集成以映射到集成请求参数或模板。

据我了解,s-function.json 中的配置直接提供给 AWS CLI,因此我指定了以下格式的请求参数: "method.request.querystring.startYear": true。但是,我收到了 Invalid mapping expression specified: true 错误。我也尝试将配置指定为"method.request.querystring.startYear": "true",结果相同。

s-function.json:

{
    "name": "myname",
    // etc...
    "endpoints": [
        {
            "path": "mypath",
            "method": "GET",
            "type": "AWS",
            "authorizationType": "none",
            "apiKeyRequired": false,
            "requestParameters": {
                "method.request.querystring.startYear": true,
                "method.request.querystring.startMonth": true,
                "method.request.querystring.startDay": true,
                "method.request.querystring.currentYear": true,
                "method.request.querystring.currentMonth": true,
                "method.request.querystring.currentDay": true,
                "method.request.querystring.totalDays": true,
                "method.request.querystring.volume": true,
                "method.request.querystring.userId": true
            },
            // etc...
        }
    ],
    "events": []
}

有什么想法吗?提前致谢!

【问题讨论】:

    标签: amazon-web-services aws-cli aws-api-gateway serverless-framework


    【解决方案1】:

    您可以将查询参数传递给您的 lambda,例如

    "requestTemplates": {
         "application/json": {
            "querystring": "$input.params().querystring"
          }
     }
    

    在 lambda 函数中访问这样的查询字符串 event.querystring

    【讨论】:

    【解决方案2】:

    确保您也使用了正确的端点。 AWS 中有两种或一些这样的类型。我的朋友过去被抓住了。

    【讨论】:

      【解决方案3】:

      看起来s-function.json 文件中的requestParameters 用于configuring the integration request section,所以我最终使用了:

      "requestParameters": {
          "integration.request.querystring.startYear" : "method.request.querystring.startYear",
          "integration.request.querystring.startMonth" : "method.request.querystring.startMonth",
          "integration.request.querystring.startDay" : "method.request.querystring.startDay",
          "integration.request.querystring.currentYear" : "method.request.querystring.currentYear",
          "integration.request.querystring.currentMonth" : "method.request.querystring.currentMonth",
          "integration.request.querystring.currentDay" : "method.request.querystring.currentDay",
          "integration.request.querystring.totalDays" : "method.request.querystring.totalDays",
          "integration.request.querystring.volume" : "method.request.querystring.volume",
          "integration.request.querystring.userId" : "method.request.querystring.userId"
      },
      

      这最终将它们自动添加到仪表板上的方法请求部分:

      然后我可以在映射模板中使用它们将它们转换为方法帖子,该帖子将作为 event 发送到我的 Lambda 函数中。现在我有一个正在使用的特定映射模板,但将来我可能会使用Alua K's suggested method 以通用方式映射所有输入,这样我就不必为每个函数配置单独的映射模板.

      【讨论】:

        【解决方案4】:

        首先,您需要执行一个 put-method 命令来创建带有查询参数的 Method-Request:

        aws apigateway put-method --rest-api-id "yourAPI-ID" --resource-id "yourResource-ID" --http-method GET --authorization-type "NONE" --no-api-key-required --request-parameters "method.request.querystring.paramname1=true","method.request.querystring.paramname2=true"
        

        在此之后,您可以执行 put-integration 命令,然后只有这将起作用。否则会报无效映射错误

        "requestParameters": {
            "integration.request.querystring.paramname1" : "method.request.querystring.paramname1",
            "integration.request.querystring.paramname2" : "method.request.querystring.paramname2",
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-05-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多