Post from controller

Hi All

I have a newbie question.

I have a RESTful controller that has a standard create method, however
I also want to POST a request to a separate external URL from this
same method.

def create
@out_message = OutMessage.new(params[:out_message])

post_to_external_url(@out_message.content)

respond_to do |format|
  if @out_message.save
    flash[:notice] = 'OutMessage was successfully created.'
    format.html { redirect_to out_message_url(@out_message) }
    format.xml  { head :created, :location =>

out_message_url(@out_message) }
else
format.html { render :action => “new” }
format.xml { render :xml => @out_message.errors.to_xml }
end
end
end

private
def post_to_external_url(content)
# code that sends a post request to
http://www.someexteranlurl.com/processme
end

What I really wish to do is create a new POST request add all my
arguments to it, POST it and then redirect back to my
‘out_message_url(@out_message)’ or index.rhtml page for this
controller.

I can see that I could use the ‘redirect_to’ method but it seems that
this can only send a GET request, how can I send a POST request?

Any clues how I should go about this, without having to resort to
creating a new view with a form and a POST action?

Many thanks,

Ben…

fredmsun wrote:

def post_to_external_url(content)
# code that sends a post request to
http://www.someexteranlurl.com/processme
end

Use Net::HTTP to connect to the other site:

http://www.ruby-doc.org/core/classes/Net/HTTP.html

Hi Mark

I actually tried this but I get a ‘stack level too deep’ error
message.
If I run the Rails console I seem to get alot of warnings about HTTP
variables already being declared, when the code hits the require ‘net/
HTTP’ call.

I then just put this piece of code into the
‘post_to_external_url(content)’ method and get the ‘stack level too
deep’ error:

Net::HTTP.get_print URI.parse(‘http://www.example.com/index.html’)

However when I run it on its own using IRB it works no problem.

I’m not sure why I get these conflicts, and I’m not sure of a ‘Rails’
way of sending new Requests from a Controller.
All I want to do is call another RESTful web service and put that
value into my model, but without going via a view.

Any suggestions will be greatly appreaciated.
Many thanks,

Ben…

On Apr 7, 10:37 am, Mark B. [email protected]

fredmsun wrote:

I actually tried this but I get a ‘stack level too deep’ error
message.
If I run the Rails console I seem to get alot of warnings about HTTP
variables already being declared, when the code hits the require ‘net/
HTTP’ call.

Where are you putting the require? What variables is it complaining
about?

I’m not sure why I get these conflicts, and I’m not sure of a ‘Rails’
way of sending new Requests from a Controller.
All I want to do is call another RESTful web service and put that
value into my model, but without going via a view.

The alternative is to use an ActiveResource model to proxy the remote
RESTful model interface. Then you can access the remote data similar to
accessing a local model…

If a working example helps, see if something like this does the
trick. It basically sets up an encrypted post to
https://gatewaybeta.fedex.com/xml.

url = ‘gatewaybeta.fedex.com
path = “/xml”
headers = {“Content-Type” => “text/xml”}
h = Net::HTTP.new(url, 443)
h.use_ssl = true
xmlrequest = “a bunch of xml…”
resp, data = h.post(path, xmlrequest, headers) #posts the request

From here, you can do whatever you want with the response status
(contained in the ‘resp’ varaible) and/or response data (contained in
the ‘data’ variable).

I don’t know much about how this affects threading, performance, etc.,
so others should feel free to chime in if I’m doing something
horrible. (Heck, I’d appreciate knowing about it!)

-Kyle

On Apr 7, 12:49 pm, Mark B. [email protected]

Hi All

Thanks so much for your help.
Mark you were right I was using require ‘net/http’ and this was
conflicting with my controller.
I removed the require altogether and it worked fine.

Although I had tried moving the ‘require’ around previously, I had
failed to notice that my Mongrel test server had crashed and so was
continually getting the ‘stack too deep’ error message.

Kyle thanks for that code snippet its most useful and good to see how
other people approach problems like these.

Many thanks,

Ben…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs