Forum: Ruby on Rails Post request from controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D128e08caeb0d93e8d8175ed33c3b750?d=identicon&s=25 bbtosurf (Guest)
on 2009-03-10 00:43
(Received via mailing list)
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
91a72b0ad3e43e3ef3a31816667ec694?d=identicon&s=25 bill walton (Guest)
on 2009-03-10 01:12
(Received via mailing list)
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/rdo...

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
D128e08caeb0d93e8d8175ed33c3b750?d=identicon&s=25 bbtosurf (Guest)
on 2009-03-10 16:23
(Received via mailing list)
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
D128e08caeb0d93e8d8175ed33c3b750?d=identicon&s=25 bbtosurf (Guest)
on 2009-03-10 16:27
(Received via mailing list)
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
91a72b0ad3e43e3ef3a31816667ec694?d=identicon&s=25 bill walton (Guest)
on 2009-03-10 17:44
(Received via mailing list)
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
This topic is locked and can not be replied to.