Post request from controller


#1

Hello guys,

I’m working on an app that posts a file to another rails application
who is in charge of running some scripts over it, create a build and
send it back again. Right now I have a form where the user selects the
type of build to run over and a build button. On my controller on the
create action I generate a build_name (random string made with UUID)
and save that into the database along with the build type specified on
the form.

So once the build_name and build_type params are saved I should make a
multipart post from my controller to the other rails_app (send the
build_name, type, callback url and the file).

That build_name will be sent to the rails app in charge of running the
scripts along with the file and a callback url. The build_name will be
used on the other app to name the file which will be sent back.

Can anyone give me any ideas on how to do this multipart post in my
controller?

Thanks a lot


#2

On Mon, 2009-03-09 at 16:42 -0700, bbtosurf wrote:

Can anyone give me any ideas on how to do this multipart
post in my controller?

See the documentation on Ruby’s Net::HTTP library
http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html

In your controller you’ll have something like…

Class MyController < ActionController
require ‘net/http’

def make_contact
my_connection = Net::HTTP.new(‘www.target.com’, 80)
reponse = my_connection.post(path_within_url, data)
#do something with response if you want
end

end

IIRC you’ll need to set a Content-Type prior to the post. Can’t remember
the setting syntax for the off the top of my head.

HTH,
Bill


#3

I have tried posting to test method and it doesn’t do anything. The
browser stays on waiting for response…and after a couple of minutes
I get this: Timeout::Error in BuildsController#create execution
expired def create require ‘net/http’ require ‘uri’ @build = Build.new
(params[:build]) if @build.save res = Net::HTTP.post_form(URI.parse
(‘http://localhost:3000/apps/1/build’), {‘q’=>‘ruby’, ‘max’=>‘50’})
end end def test puts params[:q] puts params[:max] end

sin la parte del delicioso


#4

I am sorry here it is again:

I have tried posting to test method and it doesn’t do anything. The
browser stays on waiting for response…and after a couple of minutes
I get this:

Timeout::Error in BuildsController#create execution
expired

def create
require ‘net/http’
require ‘uri’
@build = Build.new(params[:build])
if @build.save
res = Net::HTTP.post_form(URI.parse(‘http://localhost:3000/apps/1/
build’), {‘q’=>‘ruby’, ‘max’=>‘50’})
end
end

def test
puts params[:q]
puts params[:max]
end


#5

Not sure that this is the only problem, but a couple of things pop out
at me …

On Tue, 2009-03-10 at 08:26 -0700, bbtosurf wrote:

I am sorry here it is again:

I have tried posting to test method and it doesn’t do anything. The
browser stays on waiting for response…and after a couple of minutes
I get this:

Timeout::Error in BuildsController#create execution
expired

def create

I don’t think it’s the source of your current problem since the POST is
happening, but it’s more typical for the files in the require lines to
be loaded when the Class is loaded, rather than when a method is
invoked. I’d move them outside the method.

require ‘net/http’
require ‘uri’

@build = Build.new(params[:build])
if @build.save
res = Net::HTTP.post_form(URI.parse(‘http://localhost:3000/apps/1/
build’), {‘q’=>‘ruby’, ‘max’=>‘50’})
end
end

Two things:

  1. Assuming we’re inside the BuildsController, are you sure you’ve got a
    route that maps the url above (/apps/1/build) to {:controller =>
    ‘builds’, :action => ‘test’} ?. Use rake:routes to see.

  2. You’re posting to your own app. In development mode you’re probably
    running one Mongrel. That mongrel is tied up in the ‘create’ method
    waiting for your POST to get a response. It cannot process the request
    in your ‘test’ method because it’s stuck waiting, in the ‘create’
    method, until it receives the response to that request.

HTH,
Bill