KRL webhook 接收 JSON
我正在尝试为 Amazon SNS 设置 Webhook。 SNS 会将 JSON 对象发送到 webhook。根据 KRL 文档,我可以使用 event:param('name') 获取事件参数。这适用于表单编码数据,但 JSON 呢?
我向 postbin.org 发送了一个电话,这就是 postbin 报告的内容:
body {
"Message": "You have ...",
"MessageId": "958....",
"Signature": "vo3v5f....",
...
}
这是我想用 KRL 编写的内容:
rule sns_webhook {
select when webhook sometopic Type "SubscriptionConfirmation"
pre {
topic_arn = event:param("TopicARN");
signature = event:param("Signature");
message = event:param("Message");
subscribe_url = event:param("SubscribeURL");
}
if valid_signature(signature) then {
confirm_subscription(subscribe_url);
}
}
这可能适用于 HTTP 表单编码数据,但对于 JSON,我预计需要以下内容:
rule sns_json {
select when webhook sometopic
pre {
body = event:param('body').decode();
msg_type = body.pick("Type");
signature = body.pick("Signature");
...
}
if msg_type eq "SubscriptionConfirmation" && valid(signature) then
{
confirm_subscription(...);
}
}
我需要吗使用这里描述的第二种方法? event:param('body') 会从 SNS 消息中获取 JSON 数据吗?
I'm trying to set up a Webhook for Amazon SNS. SNS will send a JSON object to the webhook. Based on the KRL documentation I can get the event parameters using event:param('name'). That works for form encoded data, but what about JSON?
I sent a call to postbin.org and this is what postbin reported:
body {
"Message": "You have ...",
"MessageId": "958....",
"Signature": "vo3v5f....",
...
}
Here is what I would like to write in KRL:
rule sns_webhook {
select when webhook sometopic Type "SubscriptionConfirmation"
pre {
topic_arn = event:param("TopicARN");
signature = event:param("Signature");
message = event:param("Message");
subscribe_url = event:param("SubscribeURL");
}
if valid_signature(signature) then {
confirm_subscription(subscribe_url);
}
}
That would probably work for HTTP form encoded data, but with JSON I expect the following will be required:
rule sns_json {
select when webhook sometopic
pre {
body = event:param('body').decode();
msg_type = body.pick("Type");
signature = body.pick("Signature");
...
}
if msg_type eq "SubscriptionConfirmation" && valid(signature) then
{
confirm_subscription(...);
}
}
Do I need to use the second method described here? Will event:param('body') get the JSON data from the SNS message?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的第二个代码块非常接近。在这里,重写为使用正确的事件:param()
如果您首先有此规则,我会记得在
fired
postlude 块中添加一个last
。您还可以使用已解码的消息作为事件参数引发显式事件,并包含消息的类型,以便您可以编写显式处理不同类型的规则,而不是多次解码主体。
Your second code block is very close. Here it is, rewritten to use the correct event:param()
If you had this rule first, I would remember to add a
last
in thefired
postlude block.Instead of decoding the body multiple times, you could also raise an explicit event with the already decoded message as an event param, and include the Type of the message so that you can write rules that explicitly handle the different types.