请求参数返回 "Invalid mapping expression specified: true"

requestParameters returning "Invalid mapping expression specified: true"

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

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

requestParameters

Represents request parameters that can be accepted by Amazon API Gateway. Request parameters are represented as a key/value map, with a source as the key and a Boolean flag as the value. The Boolean flag is used to specify whether the parameter is required. A source must match the pattern method.request.{location}.{name}, where location is either querystring, path, or header. name is a valid, unique parameter name. Sources specified here are available to the integration for mapping to integration request parameters or templates.

据我了解,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": []
}

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

您可以像这样将查询参数传递给您的 lambda

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

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

确保您也使用了正确的端点。 AWS 中有两种类型或类似的类型。我的朋友过去曾被抓到过。

看起来 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"
},

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

然后我可以在映射模板中使用它们将它们变成一个方法 post,该方法将作为 event 发送到我的 Lambda 函数中。现在我有一个正在使用的特定映射模板,但我将来可能会使用 以通用方式映射所有输入,这样我就不必为每个功能。

首先,您需要执行一个 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命令,然后只有这个会起作用。否则会给出invalid mapping error

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