Webhook 验证

Webhook verification

我正在尝试在 asp.net mvc api 中编写一个 webhook 接收器 api。

但问题在于,webhook 初始化应用程序需要一种奇怪的验证方法。他们需要我添加以下代码以验证并允许我在他们的仪表板中添加我的 URL。

  `<?php if (isset($_GET['zd_echo'])) exit($_GET['zd_echo']); ?>`

你有什么建议我可以在 asp.net 中实现。到目前为止,我尝试了以下操作。 (它在邮递员中有效,但他们无法验证。)

// POST api/<controller>
    public string Post([FromBody]CallNotification value, string zd_echo)
    {
        if( zd_echo != null && zd_echo != "")
        {
            return value.zd_echo;
        }
        else
        {
           this.AddCall(value);
            return value.status_code;
        }
    }

好吧,首先我不是 Php 开发者。其次,这里有很多假设,所以这完全基于您发布的内容。

  • <?php if (isset($_GET['zd_echo'])) exit($_GET['zd_echo']); ?>
    • $_GET 是来自 query stringHTTP GET 个变量。所以这是一个 GET 请求与你 API 的预期 POST
    • 代码在 query string 中回显 zd_echo 键的值,例如http://example.com/?zd_echo=foo 将 echo/respond 与 foo 如果已设置 isset

基于以上假设:

// Just echo the value of the zd_echo key in the query string if it's set
public IHttpActionResult Get([FromUri] string zd_echo)
{
    //if not set/null return HTTP 200
    if (string.IsNullOrWhiteSpace(zd_echo))
        return Ok();

    return ResponseMessage(new HttpResponseMessage
    {
        Content = new StringContent(zd_echo)
    });
}

所以请求:http://example.com/api/webhook?zd_echo=bar 将:

  • 响应 bar 为,
  • Content-Type: text/plain; charset=utf-8

否则,http://example.com/api/webhook?zd_echo 之类的内容只会响应 HTTP/1.1 200 OK

Hth.