# Post from controller

**URL:** <https://www.ruby-forum.com/t/post-from-controller/134024>\
**Category:** Rails\
**Created:** [April 7, 2008, 12:40am UTC](https://www.ruby-forum.com/t/post-from-controller/134024 "2008-04-07T00:40:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fredmsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fredmsun](https://www.ruby-forum.com/u/fredmsun)\
**Post date:** [April 7, 2008, 12:40am UTC](https://www.ruby-forum.com/t/post-from-controller/134024/1 "2008-04-07T00:40:17Z")

</div>

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](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…

---

<div class="post-metadata">

**Author:** ![fredmsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fredmsun](https://www.ruby-forum.com/u/fredmsun)\
**Post date:** [April 7, 2008, 11:37am UTC](https://www.ruby-forum.com/t/post-from-controller/134024/2 "2008-04-07T11:37:18Z")

</div>

fredmsun wrote:

> def post\_to\_external\_url(content)  
> # code that sends a post request to  
> [http://www.someexteranlurl.com/processme](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](http://www.ruby-doc.org/core/classes/Net/HTTP.html)

---

<div class="post-metadata">

**Author:** ![fredmsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fredmsun](https://www.ruby-forum.com/u/fredmsun)\
**Post date:** [April 7, 2008, 12:29pm UTC](https://www.ruby-forum.com/t/post-from-controller/134024/3 "2008-04-07T12:29:04Z")

</div>

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(‘[Example Domain](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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![fredmsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fredmsun](https://www.ruby-forum.com/u/fredmsun)\
**Post date:** [April 7, 2008, 7:49pm UTC](https://www.ruby-forum.com/t/post-from-controller/134024/4 "2008-04-07T19:49:23Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![fredmsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fredmsun](https://www.ruby-forum.com/u/fredmsun)\
**Post date:** [April 7, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/post-from-controller/134024/5 "2008-04-07T21:22:50Z")

</div>

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](https://gatewaybeta.fedex.com/xml).

url = ‘[gatewaybeta.fedex.com](http://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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![fredmsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fredmsun](https://www.ruby-forum.com/u/fredmsun)\
**Post date:** [April 9, 2008, 4:11pm UTC](https://www.ruby-forum.com/t/post-from-controller/134024/6 "2008-04-09T16:11:10Z")

</div>

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…
