Submit POST data from controller to another website in Rails

Alex.Bullard picture Alex.Bullard · Jul 28, 2009 · Viewed 73.4k times · Source
  1. User submits a form with some basic data.

  2. The data is received and treated by an action in the controller and more information that needs to remain private is added.

  3. Then I need to send a post request to an external website with all of the combined data from the controller.

What is the best way to do this?

Answer

Vlad Zloteanu picture Vlad Zloteanu · Jul 28, 2009

The simpliest way is using ruby core library:

require "uri"
require "net/http"

params = {'box1' => 'Nothing is less important than which fork you use. Etiquette is the science of living. It embraces everything. It is ethics. It is honor. -Emily Post',
'button1' => 'Submit'
}
x = Net::HTTP.post_form(URI.parse('http://www.interlacken.com/webdbdev/ch05/formpost.asp'), params)
puts x.body

Pro Tip: Do an asynchronous request, using a gem like delayed_job or background_rb