# Ruby cgi, add params and redirect

**URL:** <https://www.ruby-forum.com/t/ruby-cgi-add-params-and-redirect/149775>\
**Category:** Ruby\
**Created:** [September 29, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/ruby-cgi-add-params-and-redirect/149775 "2008-09-29T18:53:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [September 29, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/ruby-cgi-add-params-and-redirect/149775/1 "2008-09-29T18:53:25Z")

</div>

I’m looking for a way to add parameters to a cgi object and then  
redirect the user to another cgi script off site.

* * *

require “cgi”

cgi = CGI.new

cgi.params[‘new\_param’] = “new\_value”  
…

## cgi.header(“status” =\> “302”, “location” =\> “offsite\_script.cgi”)

I can add parameters no problem as above, but the offsite script isn’t  
receiving the added parameters. Is this because the redirect is  
happening before the parameters are being added? Is there another way to  
do this?

Thanks,  
Dave

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [September 29, 2008, 10:47pm UTC](https://www.ruby-forum.com/t/ruby-cgi-add-params-and-redirect/149775/2 "2008-09-29T22:47:21Z")

</div>

OK, so the above isn’t working because 302 is being return to the  
client, which is re-sending the original params to the redirect location  
(offsite\_script.cgi)

I’ve played around with Net:HTTP this seems to be sending the data but  
the user is oblivious to this. Is there a way to add params to the cgi  
hash and forward the user on? Essentially I’m looking for this script  
that adds params to be transparent to the user.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [September 29, 2008, 11:47pm UTC](https://www.ruby-forum.com/t/ruby-cgi-add-params-and-redirect/149775/3 "2008-09-29T23:47:36Z")

</div>

You should use cookies (maybe with cgi session), or add the paramenters  
to the  
url, for example:

puts cgi.header(‘status’ =\> ‘REDIRECT’,  
‘location’ =\> ‘offsite\_script.cgi?new\_param=new\_value’)

[http://www.ruby-doc.org/core/classes/CGI/Session.html](http://www.ruby-doc.org/core/classes/CGI/Session.html)  
[http://www.ruby-doc.org/core/classes/CGI/Cookie.html](http://www.ruby-doc.org/core/classes/CGI/Cookie.html)

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [September 30, 2008, 7:22pm UTC](https://www.ruby-forum.com/t/ruby-cgi-add-params-and-redirect/149775/4 "2008-09-30T19:22:30Z")

</div>

Good ideas, but the offside script needs the post method and doesn’t  
accept parameters passed through the URL. I also don’t have the ability  
to change the offsite script so cookies are out as well.

Is there no way to forward a user on? This is easy enough to do on the  
client side with Javascript, but if a user has scripting turned off none  
of it will work.

Dave

Martin B. wrote:

> You should use cookies (maybe with cgi session), or add the paramenters  
> to the  
> url, for example:
> 
> puts cgi.header(‘status’ =\> ‘REDIRECT’,  
> ‘location’ =\> ‘offsite\_script.cgi?new\_param=new\_value’)
> 
> [http://www.ruby-doc.org/core/classes/CGI/Session.html](http://www.ruby-doc.org/core/classes/CGI/Session.html)  
> [http://www.ruby-doc.org/core/classes/CGI/Cookie.html](http://www.ruby-doc.org/core/classes/CGI/Cookie.html)
