簡體   English   中英

Webhook發布到我的rails應用程序,控制器應該如何響應?

[英]Webhook POSTing to my rails app, how should controller respond?

我設置了Mandrill網絡掛鈎,以便每次我的應用嘗試向無效地址發送電子郵件時,Mandrill都會向我的應用發送帶有錯誤的POST。

我已經設置了一個模型/控制器,以將該帖子捕獲為EmployeeDataError類。

我的問題是,由於Mandrill只是將POST發送到我的應用程序,而無需進行其他操作,因此控制器操作是否應該包含response_to塊? create動作當前響應的只是來自Rails腳手架的內容,但是此response_to塊似乎適合我站點上的用戶,而不適合外部服務。

  def create
    employee = Employee.find_by_email(params[:email])
    @employee_data_error = EmployeeDataError.new(employee_id: employee.id)

    respond_to do |format|
      if @employee_data_error.save
        format.html { redirect_to @employee_data_error, notice: 'Employee data error was successfully created.' }
        format.json { render action: 'show', status: :created, location: @employee_data_error }
      else
        format.html { render action: 'new' }
        format.json { render json: @employee_data_error.errors, status: :unprocessable_entity }
      end
    end
  end

這取決於您要使用的方法


要求

請求的methodPOST )與Rails的respond_to塊無關,因為它處理請求的typexhr / http )。 區別在於無論是否是GET,POST,PATCH等,response_to塊仍將運行

我建議您簡單地將:status返回給Mandrill,因為這樣可以節省CPU功耗。 這樣(假設Mandrill發送JSON請求):

    respond_to do |format|
      if @employee_data_error.save
        format.html { redirect_to @employee_data_error, notice: 'Employee data error was successfully created.' }
        format.json { render nothing: true, status: :200 }
      else
        format.html { render action: 'new' }
        format.json { render nothing: true, status: :500 }
      end
    end

希望這會有所幫助嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM