首页 > 解决方案 > rails 在两个 localhost 之间接收 webhook

问题描述

我有一个发送带有数据的 webhook (localhost:3000) 的 rails 应用程序。之前,我使用在线 webhook 监视器 (Pipedream),但现在我想构建第二个 rails 应用程序来接收数据 (localhost:3000 -> localhost:3300)。我已经创建了一个路由 /webhook/receive 和它的控制器,但从 localhost:3000 什么也没得到。

我做错了什么?

那是我的第一个应用程序:

def send_create_webhook(webhook_url)
 company = User.find_by(email: 'test1@test.de').company_account
 webhook_url = 'tcp://0.0.0.0:3300/data/receive'

 SendWebhookJob.perform_later(webhook_url, {
      subscription: {
          type: "#{@tool.title} successfully subscribed",
          date: "#{Time.zone.now}",
          subscribed: Time.now.strftime("%Y-%d-%m %H:%M:%S %Z"),
          user: {
              company: "#{company.title}",
              name: current_user.name,
              email: current_user.email,
          }
      }
  })
end


class SendWebhookJob < ApplicationJob
  queue_as :default

  WEBHOOK_RETRIES = 3

  def perform(webhook_url, data, retry_count = 0)
    p "COUNT #{retry_count} : #{Time.zone.now} : Sending data to #{webhook_url}"

    # make a request
    begin
      response = HTTP.post(webhook_url, json:data)
      successful = response.code == 200
    rescue StandardError => e
      successful = false
    end

    if successful
      p "SUCCESSFUL WEBHOOK"
    elsif retry_count < WEBHOOK_RETRIES
      wait = 2 ** retry_count
      SendWebhookJob.set(wait: wait.seconds).perform_later(webhook_url, data,retry_count + 1)
    end
  end
end

我刚刚创建的第二个应用程序:

resources :data do
 get :receive
 post :receive
end

def DataController < ApplicationController
 require "http"

 def receive
  response = HTTP.get('tcp://0.0.0.0:3000')
 end

end

标签: ruby-on-railsrubyapiwebhooks

解决方案


推荐阅读