# Posting a form from Rails programatically

**URL:** <https://www.ruby-forum.com/t/posting-a-form-from-rails-programatically/166915>\
**Category:** Rails\
**Created:** [April 30, 2009, 9:23pm UTC](https://www.ruby-forum.com/t/posting-a-form-from-rails-programatically/166915 "2009-04-30T21:23:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![niczm25](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@niczm25](https://www.ruby-forum.com/u/niczm25)\
**Post date:** [April 30, 2009, 9:23pm UTC](https://www.ruby-forum.com/t/posting-a-form-from-rails-programatically/166915/1 "2009-04-30T21:23:26Z")

</div>

How would I go about creating and posting a form POST request from  
inside some Rails code?  
The use case I have is that I have received a form request, and I  
would like to forward this request on to a third party with my  
parameters intact.

I should add that I want to redirect the user out to the third party  
with the form too. Essentially I want it to appear to the user that  
they’ve submitted the form to the third party directly.

---

<div class="post-metadata">

**Author:** ![niczm25](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@niczm25](https://www.ruby-forum.com/u/niczm25)\
**Post date:** [April 30, 2009, 10:18pm UTC](https://www.ruby-forum.com/t/posting-a-form-from-rails-programatically/166915/2 "2009-04-30T22:18:13Z")

</div>

Neil M. wrote:

> How would I go about creating and posting a form POST request from  
> inside some Rails code?  
> The use case I have is that I have received a form request, and I  
> would like to forward this request on to a third party with my  
> parameters intact.
> 
> I should add that I want to redirect the user out to the third party  
> with the form too. Essentially I want it to appear to the user that  
> they’ve submitted the form to the third party directly.

Check out mechanize:

[http://mechanize.rubyforge.org/mechanize/GUIDE\_rdoc.html](http://mechanize.rubyforge.org/mechanize/GUIDE_rdoc.html)

---

<div class="post-metadata">

**Author:** ![niczm25](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@niczm25](https://www.ruby-forum.com/u/niczm25)\
**Post date:** [April 30, 2009, 10:19pm UTC](https://www.ruby-forum.com/t/posting-a-form-from-rails-programatically/166915/3 "2009-04-30T22:19:58Z")

</div>

7stud – wrote:

> I should add that I want to redirect the user

whoops.

---

<div class="post-metadata">

**Author:** ![niczm25](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@niczm25](https://www.ruby-forum.com/u/niczm25)\
**Post date:** [May 1, 2009, 3:43pm UTC](https://www.ruby-forum.com/t/posting-a-form-from-rails-programatically/166915/4 "2009-05-01T15:43:33Z")

</div>

> I should add that I want to redirect the user out to the third party  
> with the form too. Essentially I want it to appear to the user that  
> they’ve submitted the form to the third party directly.

You can either use an HTTP 307 response ([HTTP - Hypertext Transfer Protocol Overview](http://www.w3.org/Protocols/)  
rfc2616/rfc2616-sec10.html#sec10.3.8) which, according to the  
standard, should re-send the original request to the given location  
(keeping the original method and data), but browsers require  
confirmation from the user (most browsers - an unfriendly dialog),  
some browser-specific quirks may occur as well. Except from this,  
that’s the ‘standard’ method for your use case.

You can also use any Ruby HTTP client to send the request from your  
server, wait for the response, and, basing on it, send your response  
(maybe a redirect) to the client. This solution is more complicated to  
implement, and if it’s a non-trivial third party form, some issues  
with cookies/sessions may appear.

Keep us updated on your progress.

–  
Bye,  
Olek

---

<div class="post-metadata">

**Author:** ![niczm25](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@niczm25](https://www.ruby-forum.com/u/niczm25)\
**Post date:** [May 1, 2009, 4:15pm UTC](https://www.ruby-forum.com/t/posting-a-form-from-rails-programatically/166915/5 "2009-05-01T16:15:11Z")

</div>

I’ve tried a 307, which seems to work with Firefox (with a warning, as  
you say), but Safari (4) seems to miss the re-post. I’ve not checked  
IE, as I’ve not got a copy to hand.

Neil
