如何正确验证Rails中的webhook URL?

时间:2014-09-09 04:38:44

标签: ruby-on-rails ruby controller webhooks

我正在集成API并为我的应用设置webhook。 API提供商要求新的webhook网址通过返回发布到新创建的webhook网址的令牌来进行一次性验证。

我以前没有使用过webhooks,似乎无法验证网址。以下是我的控制器和路线:

控制器:

    class WebhooksController < ApplicationController

      skip_before_filter :verify_authenticity_token

      def receive
        raw_body = request.body.read
        json = JSON.parse raw_body
        # Return verification token only once for intitial webhook setup.
        token = json['data']['token']

        # The webhook normally only requires a 200/ok in return
        # but for the intial setup I want to return the received token in response.
        render :json => token
      end

    end

路线:

    post '/webhooks/receive' => 'webhooks#receive'

谢谢!

1 个答案:

答案 0 :(得分:0)

这真的取决于你想要归来的东西。它应该是这样的JSON对象吗?

{ token: 'a token here' } 

然后你可能想要:

render :json => { token: token }