gpt4 book ai didi

c# - 如何从 Amazon API Gateway 将参数从 POST 传递到 AWS Lambda

转载 作者:行者123 更新时间:2023-11-30 23:11:33 24 4
gpt4 key购买 nike

我正在尝试使用此解决方案将我的 Api-Gateway 映射应用程序/x-www-form-urlencoded 转换为 json: How to pass a params from POST to AWS Lambda from Amazon API Gateway

但到目前为止,我的 lambda 被成功触发,只有我的 request.body 始终为 null。如果有人知道如何使用 .net-core c# 处理该问题,我将不胜感激。

到目前为止,这是我的无服务器 lambda 函数的样子我收到了时间戳,但没有关于 request.body 的内容

public async Task<APIGatewayProxyResponse> Post(APIGatewayProxyRequest request, ILambdaContext context)
{
var webHook = new WebHookClient("https://{urlHiddenForObviousReasons}");
var body = new BodyModel
{
Content = $"Trigger @ {DateTime.UtcNow}, {request.Body}"
};
await webHook.PostMessage(body);
var response = new APIGatewayProxyResponse
{
StatusCode = (int)HttpStatusCode.OK,
Body = "Alert received.",
Headers = new Dictionary<string, string> { { "Content-Type", "text/plain" } }
};

return response;
}

请注意,如果我使用代理集成而不是传递表单值,我想使用映射,这样我就可以让两个客户端使用相同的 api 和不同的 post 方法,并且让 lambda 只解析 json。以这种方式最终传递表单值的设置示例:key=1&steamid=1&notetitle=ARK-ALARM%3A+06%2F17+%40+16%3A24+on+Paleolithic+Ark+-+The+Island%2C+ALARM+ 'Base'+IN+'The+Hidden+Lake'+WAS+TRIPPED!&message=...

enter image description here

最佳答案

您需要添加一个 Mapping Template .例如:

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

将其添加到您的 POST 方法的集成请求部分。 AWS API Gateway Integration Request

一个很好的例子是here

另一个对我有用的更复杂的例子:

#set($allParams = $input.params())
{
"body-json" : $input.json('$'),
"params" : {
#foreach($type in $allParams.keySet())
#set($params = $allParams.get($type))
"$type" : {
#foreach($paramName in $params.keySet())
"$paramName" : "$util.escapeJavaScript($params.get($paramName))"
#if($foreach.hasNext),#end
#end
}
#if($foreach.hasNext),#end
#end
}
}

我无法对 C# 代码发表评论,因为我已经习惯了 Javascript 和 Python,但它似乎很好。

关于c# - 如何从 Amazon API Gateway 将参数从 POST 传递到 AWS Lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44617466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com