【问题标题】:pass QueryString Paramaters from API Gateway to AWS Lambda c#将查询字符串参数从 API Gateway 传递到 AWS Lambda c#
【发布时间】:2017-11-14 04:57:10
【问题描述】:
我正在尝试使用 APIGateway 调用 AWS Lambda,它会返回 HTML 代码。当我不传递任何参数时它工作正常,但我想传递一些 QueryString 参数并在 Lambda 中使用它们。我在 C# 中有我的 Lambda,我看到从 API 传递的参数
response from API
"headers": {},
"QueryStringParameters": {
"Environment": "xzc"
},
"PathParameters": {}
}
在 Lambda 中,APIGatewayProxyRequest 为 null
API Lambda
public string FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
如何在 C# 中读取 AWS Lambda 中的查询字符串参数
【问题讨论】:
标签:
amazon-web-services
aws-lambda
aws-sdk
aws-api-gateway
【解决方案1】:
看来您只需在 API Gateway 资源配置中的 Integration Request 中检查 Use Lambda Proxy integration。
还有
你应该包括:
using Amazon.Lambda.APIGatewayEvents;
你的处理函数头应该是这样的:
public APIGatewayProxyResponse FunctionHandler( APIGatewayProxyRequest input, ILambdaContext context)
然后您可以通过以下方式访问您的查询字符串参数:
input.QueryStringParameters
【解决方案2】:
解释超过 1 个输入参数,因为有时这对开发人员来说也是一个问题:
步骤 01:这应该是您的 C-Sharp 方法
public string FunctionHandler(string strEnvironmentA, string strEnvironmentB, ILambdaContext context);
步骤 02:在 API > GET Method Execution > Method Request 中添加查询字符串参数
- strEnvironmentA
- strEnvironmentB
步骤 03:在 API > GET Method Execution > Integration Request > Body Mapping Template 添加这个 application/json 模板
"$input.params('strEnvironmentA')"
"$input.params('strEnvironmentB')"
【解决方案3】:
做这样的事情:
public string FunctionHandler(string input, ILambdaContext context);
然后您可以在请求正文中传递输入而不是查询字符串参数。
【解决方案4】:
if (request.QueryStringParameters != null)
{
queryStringParameters = request.QueryStringParameters;
foreach (var item in queryStringParameters)
{
Console.WriteLine($"QueryStringParameter - " + item.Key + ":" + item.Value);
}
}