例如,如果我们想用

得到-用户?name =鲍勃

or

获取/用户/鲍勃

如何将这两个例子作为参数传递给Lambda函数?

我在文档中看到了一些关于设置“映射from”的内容,但我在API Gateway控制台中找不到该设置。

method.request.path。在方法请求页面中定义了一个名为parameter-name的路径参数。 method.request.querystring。parameter-name用于在方法请求页面中定义的名为parameter-name的查询字符串参数。

尽管我定义了一个查询字符串,但我没有看到这两个选项。


当前回答

在阅读了其中几个答案后,我在2018年8月使用了几个答案的组合,通过python 3.6的lambda检索查询字符串参数。

首先,我去API网关->我的API ->资源(在左边)->集成请求。在底部,选择映射模板,然后为内容输入application/json。

接下来,选择Amazon提供的方法请求传递模板,并选择保存和部署API。

然后,lambda event['params']是你访问所有参数的方式。查询字符串:event['params']['querystring']

其他回答

为了获取查询参数,您可以像这样在queryStringParameters对象中获取它们

const name = event.queryStringParameters.name;

第二个是一个干净的URL。如果你的路径是/user/{name},你可以像这样从pathParameters对象中获取值

const name = event.pathParameters.name;

让它工作的步骤是:

在API网关控制台中…

Go to Resources -> Integration Request Click on the plus or edit icon next to the templates dropdown (odd I know since the template field is already open and the button here looks greyed out) Explicitly type application/json in the content-type field even though it shows a default (if you don't do this it will not save and will not give you an error message) put this in the input mapping { "name": "$input.params('name')" } click on the check box next to the templates dropdown (I'm assuming this is what finally saves it)

现在您应该能够为Lambda使用新的代理集成类型来自动获得标准形状的完整请求,而不是配置映射。

见:http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html api-gateway-set-up-lambda-proxy-integration-on-proxy-resource

exports.handler = async (event) => {
    let query = event.queryStringParameters;
    console.log(`id: ${query.id}`);
    const response = {
        statusCode: 200,
        body: "Hi",
    };
    return response;
};

得到-用户?name =鲍勃

{
    "name": "$input.params().querystring.get('name')"
}

获取/用户/鲍勃

{
    "name": "$input.params('name')"
}