例如,如果我们想用

得到-用户?name =鲍勃

or

获取/用户/鲍勃

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

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

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

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


当前回答

Python 3.8 with boto3 v1.16v - 2020年12月

配置路由时,必须配置“API网关”接受路由。否则,除了基本路由之外,其他所有内容都将以{missing auth令牌}或其他内容结束…

一旦你配置API网关接受路由,确保你启用了lambda代理,这样事情就会更好地工作,

要访问路由,

new_route = event['path'] # /{some_url}

访问查询参数

query_param = event['queryStringParameters'][{query_key}]

其他回答

让它工作的步骤是:

在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)

对我来说有效的方法是

转到集成请求 单击URL查询字符串参数 单击“添加查询字符串” 在name字段中放入查询名称,这里是“name” 在Mapped From字段中,放入method.request.querystring.name

参考医生: https://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-lambda.html#api-as-lambda-proxy-expose-get-method-with-path-parameters-to-call-lambda-function

需要修改映射模板

你可以使用Lambda作为“Lambda代理集成”,参考这个[https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html#api-gateway-proxy-integration-lambda-function-python],这个Lambda可用的选项是

对于Nodejs Lambda ”事件。头”、“事件。pathParameters”、“事件。身体”、“event.stageVariables”, 和“event.requestContext”

对于Python Lambda 事件['headers']['parametername']等

查询字符串可以直接在lambda中用javascript解析

GET /user?name =鲍勃

 var name = event.queryStringParameters.name;

但是,这并不能解决GET用户/bob的问题。