17

Rails API 通常喜欢这样的数组查询参数:

example.com?colors[]=cyan&colors[]=magenta&colors[]=yellow&colors[]=black

我如何将其映射到 lambda 函数?

4

5 回答 5

18

API Gateway 代理现在支持查询字符串中具有相同名称的多个参数。它们在multiValueQueryStringParameters字典中传递。

例如

 GET /api/path/?param=value&param=othervalue&something=thing

将生成以下请求:

{
    "resource": "/{proxy+}",
    "path": "/ap/path/",
    "httpMethod": "GET",
    "queryStringParameters": {
        "param": "othervalue",  # only the last value is kept here
        "something": "thing"
    },
    "multiValueQueryStringParameters": {
        "param": [
            "value",
            "othervalue"
        ],
        "something": [
            "thing"
        ]
    },
    "pathParameters": {
        "proxy": "api/path"
    },
    # etc
}

请参阅文档

于 2018-10-17T15:29:01.667 回答
10

一点点试验和错误表明它是这样的:

example.com?colors=['cyan','magenta','yellow','black']

于 2015-08-05T22:51:14.527 回答
8

您不能做的一件事是按照https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-known-issues.html重复查询字符串参数键:

不支持重复的标头。

API Gateway 将参数映射到 JSON 对象,因此只要每个项目都有自己的唯一键,您就不会受到任何破坏。

你的选择很好,但我头脑风暴了一些其他的选择。您可以执行类似在方括号中添加索引的操作:

example.com?colors[0]=cyan&colors[1]=magenta&colors[2]=yellow&colors[3]=black

如果您愿意从 GET 转换为 POST,则可以在 POST 正文中发送查询字符串。然后您可以自己解析原始查询字符串。您甚至可以在此处重复参数键,因为您正在解析它。

?colors=cyan&colors=magenta&colors=yellow&colors=black

另一个 POST 选项是在 POST 正文中发送 JSON 数组。我知道 POST 不像 GET 那样容易使用,但它是一种选择。

["cyan","magenta","yellow","black"]
于 2017-05-26T00:37:34.060 回答
3

正如@kjs3 回答中所讨论的,API Gateway 不支持查询字符串或标头中的重复参数。但是,您可以使用单参数数组格式。

支持重复的参数键+值是我们确实想做的事情,但我目前没有 ETA。

于 2017-05-30T16:13:23.127 回答
0

如果有人在 AWS Api Gateway 中使用 OpenAPI 3.0 规范,那么您需要x-amazon-apigateway-integration像这样定义它。

x-amazon-apigateway-integration:
        ...
        requestParameters:
            integration.request.querystring.colors: method.request.multivaluequerystring.colors
        ...
于 2022-01-27T14:04:44.360 回答