繁体   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