KRL webhook 接收 JSON

发布于 2024-11-14 03:51:36 字数 1146 浏览 5 评论 0原文

我正在尝试为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽手叙旧 2024-11-21 03:51:36

您的第二个代码块非常接近。在这里,重写为使用正确的事件:param()

rule sns_json {
  select when webhook sometopic
  pre {
    body = event:param('request_body').decode();
    msg_type = body.pick("Type");
    signature = body.pick("Signature");
    ...
  }
  if msg_type eq "SubscriptionConfirmation" && valid(signature) then
  {
    confirm_subscription(...);
  }
}

如果您首先有此规则,我会记得在 fired postlude 块中添加一个 last

您还可以使用已解码的消息作为事件参数引发显式事件,并包含消息的类型,以便您可以编写显式处理不同类型的规则,而不是多次解码主体。

Your second code block is very close. Here it is, rewritten to use the correct event:param()

rule sns_json {
  select when webhook sometopic
  pre {
    body = event:param('request_body').decode();
    msg_type = body.pick("Type");
    signature = body.pick("Signature");
    ...
  }
  if msg_type eq "SubscriptionConfirmation" && valid(signature) then
  {
    confirm_subscription(...);
  }
}

If you had this rule first, I would remember to add a last in the fired 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文