简体   繁体   中英

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

I have a Mandrill web hook set up so that every time my app tries to send an email to an invalid address, Mandrill will send a POST to my app with the error.

I have set up a model/controller to capture this post as a EmployeeDataError class.

My question is, since Mandrill is just sending a POST to my app with no need to go elsewhere, should the controller action even have a respond_to block? What the create action currently responds with is just what came from the Rails scaffold, but this respond_to block seems suited for a user on my site and not an external service.

  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

It depends on what you want the method to do


Requests

The method of the request ( POST ) is irrelevant to Rails' respond_to block, as this deals with the type of request ( xhr / http ). The difference is the respond_to block is still going to run regardless of whether it's a GET, POST, PATCH etc

I would recommend simply returning a :status to Mandrill, as this will save CPU power all around; like this (assuming Mandrill sends a JSON request):

    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

Hopefully this will help?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM