Parsing POST and GET variables simultaneously?

Isn’t it possible to get variables from POST and GET simultaneously?
Consider the script test.rbx below:

require 'cgi’
cgi = CGI.new
print “HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n”

if cgi.params.empty? then
puts ""
puts ""
puts ""
puts ""
else
print cgi.params.inspect
end

Calling test.rbx?valueFromGet=here gives me:
{“valueFromPost”=>[“there”]}

Calling text.rbx and entering here “here” in the input gives me:
{“valueFromPost”=>[“here”]}

…where I would have expected:
{“valueFromPost”=>[“here”],“valueFromPost”=>[“there”]}

Any ideas on how to get the latter?

···


David Heinemeier Hansson,
http://www.loudthinking.com/ – Thoughts for free
http://www.nextangle.com/ – Thoughts for hire

Argh… bloody copy/paste.

Calling test.rbx?valueFromGet=here gives me:
{“valueFromPost”=>[“there”]}

That should have been {“valueFromGet”=>[“here”]}

Calling text.rbx and entering here “here” in the input gives me:
{“valueFromPost”=>[“here”]}

Right.

…where I would have expected:
{“valueFromPost”=>[“here”],“valueFromPost”=>[“there”]}

{“valueFromGet”=>[“here”],“valueFromPost”=>[“there”]}

···


David Heinemeier Hansson,
http://www.loudthinking.com/ – Thoughts for free
http://www.nextangle.com/ – Thoughts for hire

David Heinemeier Hansson wrote:

Isn’t it possible to get variables from POST and GET simultaneously?

Aren’t they two distinct request types? You either do one or the other?

I suppose you could POST to a URL that included a query string, grab the
POST data and explicitly parse the query string (the PATH_INFO cgi
variable, or maybe SCRIPT_NAME).

James

Not really, since REQUEST_METHOD can only be set to one of ‘GET’ or ‘POST’,
and cgi.rb takes its cue from that.

Even with REQEUT_METHOD set to POST, you can still get the query string via
cgi.query_string, or use ‘/’ instead of the ‘?’, in which case it shows up
as cgi.path_info:

<form action="test.rbx/valueFromGet=here" method="post">

Either way, you have to parse the string yourself, though. Identify
the name=value pairs, decode the '+'s and '%xx’s into spaces and other
unprintable characters, etc.

-Mark

···

On Mon, Jul 21, 2003 at 12:39:08AM +0900, David Heinemeier Hansson wrote:

Isn’t it possible to get variables from POST and GET simultaneously?

David Heinemeier Hansson wrote:

Isn’t it possible to get variables from POST and GET simultaneously?

Aren’t they two distinct request types? You either do one or the other?

One or the other, but:

I suppose you could POST to a URL that included a query string, grab the
POST data and explicitly parse the query string (the PATH_INFO cgi
variable, or maybe SCRIPT_NAME).

It’s not post and get at the same time, but a post can have a query
string as well as post-data. There’s also nothing that says that post
has to take a URLencoded string – some protocols on top of HTTP (DAV &
company) use XML posted to a URL.

···

On Sun, 2003-07-20 at 11:10, james_b wrote:

Aren’t they two distinct request types? You either do one or the other?

The problem is that I’m using mod_rewrite to do pretty URLs, like
“/board/show_topics” gets rewritten to
“/app/controllers/board.rbx?action=show_topics”. So now when I post to
“/board/create_topic”, I don’t get the action variable.

I suppose you could POST to a URL that included a query string, grab
the POST data and explicitly parse the query string (the PATH_INFO cgi
variable, or maybe SCRIPT_NAME).

Ahh. That’s a good idea – as a work-around. I was surprised, though,
coming from PHP, that I wouldn’t just get it all.

Either way, you have to parse the string yourself, though. Identify
the name=value pairs, decode the '+'s and '%xx’s into spaces and other
unprintable characters, etc.

class CGI
def query_params
params = {}

     ENV['QUERY_STRING'].split("&").each { |p|
         k, v = p.split("=")
         params[CGI.unescape(k)] = CGI.unescape(v)
     }

     params
 end

end

…that does the trick. God, I love Ruby. May I never stray back into
PHP-land again :slight_smile:

Somehow, despite the explicit instructions in my subject line,
this has turned into YAEPT. The sheer power of that
topic is amazing; it’s like a black hole sucking in all threads
which get too close to its event horizon. Obviously my
English phoneme thread started out way too close; I was orbiting
the darn thing. Anyway . .

AFAIK, all Americans pronounce “Mary” as /meiri/;
they pronounce “merry” and “marry” as /meiri/ also. For me they are
/mEri/ and /m&ri/ respectively.

Most definitely not. For me, my wife, our tenant, and most of my
friends and coworkers, all three of them are [mE`r\i], with the
same vowel (apart from the rhoticization) as “met”, “bled”, etc;
as distinguished from the vowel of “mate”, “blade”, etc.
Other a+r words have the same “short-e” vowel: “bare” (which
doesn’t rhyme with the trade name Bayer, which has /ei/),
“care”, “dare”, “fair”, “fare”, “hair”, “hare”, etc.

As for “yeah”, I’ve always thought it was a peculiar and perverse
spelling for the word /jV/.

Again, not hereabouts. “Yeah” is pronounced /j{/; it seems to be a
exception to the rule that disallows lax open monosyllables in English.

If I speak it in extreme slow motion,
it comes out /je:::::V/, but that’s bizarre.

Here it’s more like /j{::::ijV/.

-Mark

From markjreed@mail.com Sun Jul 20 17:14:14 2003

···

On Sat, Jul 19, 2003 at 11:05:59PM -0400, John Cowan wrote:
From: “Mark J. Reed” markjreed@mail.com
To: David Heinemeier Hansson david@loudthinking.com
Subject: Re: Parsing POST and GET variables simultaneously?
References: 20030720172047.GA28762@mulan.thereeds.org 2878AB4E-BADA-11D7-9A77-000A959C001E@loudthinking.com
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: 2878AB4E-BADA-11D7-9A77-000A959C001E@loudthinking.com
X-Julian-Day: JD 2452841.38079
X-Roman-Date: ante diem XIII =?utf-8?Q?Kalend=C4=81s_Aug?=
=?utf-8?B?dXN0xIFz?= A.U.C. MMDCCLVI
X-Mutt-References: 2878AB4E-BADA-11D7-9A77-000A959C001E@loudthinking.com
X-Mutt-Fcc: =sent
Status: RO
Content-Length: 943
Lines: 37

On Mon, Jul 21, 2003 at 02:47:37AM +0900, David Heinemeier Hansson wrote:

class CGI
def query_params
params = {}

    ENV['QUERY_STRING'].split("&").each { |p|
        k, v = p.split("=")
        params[CGI.unescape(k)] = CGI.unescape(v)
    }

    params
end

end

…that does the trick. God, I love Ruby. May I never stray back into
PHP-land again :slight_smile:

May your wish come true. :slight_smile: And that solution will indeed work
most of the time. However, to be perfectly compliant with modern
protocols, you need to split on either ‘&’ or ‘;’. And it’d be
better to use the query_string method than to directly access the
environment variable:

class CGI
def query_params
params = {}

     query_string.split(/[&;]/).each { |p|
         k, v = p.split('=')
         params[CGI.unescape(k)] = CGI.unescape(v)
     }

     params
 end

end

-Mark

Eep! Holy Wrong Newsgroup, Batman!

It did include the stuff that was meant to go here, though, which
I hereby cut to.

Sorry about that.

class CGI
def query_params
params = {}

    ENV['QUERY_STRING'].split("&").each { |p|
        k, v = p.split("=")
        params[CGI.unescape(k)] = CGI.unescape(v)
    }

    params
end

end

…that does the trick. God, I love Ruby. May I never stray back into
PHP-land again :slight_smile:

May your wish come true. :slight_smile: And that solution will indeed work
most of the time. However, to be perfectly compliant with modern
protocols, you need to split on either ‘&’ or ‘;’. And it’d be
better to use the query_string method than to directly access the
environment variable:

class CGI
def query_params
params = {}

     query_string.split(/[&;]/).each { |p|
         k, v = p.split('=')
         params[CGI.unescape(k)] = CGI.unescape(v)
     }

     params
 end

end

-Mark

···

On Mon, Jul 21, 2003 at 02:47:37AM +0900, David Heinemeier Hansson wrote:

Mark J. Reed wrote:

Eep! Holy Wrong Newsgroup, Batman!

It did include the stuff that was meant to go here, though, which
I hereby cut to.

Sorry about that.

Darn, and that was so much more interesting then GETs and POSTs :).
For a while you had me searching through the list to figure out what
YAEPT is. (Yet Another English Phonetis Thread ? )
:slight_smile:
V.-

···

http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.

What mailing list (newsgroup?) does that belong to?
It’s interesting.

···

On Mon, Jul 21, 2003 at 06:37:24AM +0900, Mark J. Reed wrote:

Eep! Holy Wrong Newsgroup, Batman!


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

How should I know if it works? That’s what beta testers are for. I
only coded it.
– Attributed to Linus Torvalds, somewhere in a posting

Yeah, I thought it was interesting, too… meant for
sci.lang or where? Haven’t read that one in ages…

Hal

···

----- Original Message -----
From: “Damphyr” damphyr@freemail.gr
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Sunday, July 20, 2003 4:55 PM
Subject: Re: Parsing POST and GET variables simultaneously?

Mark J. Reed wrote:

Eep! Holy Wrong Newsgroup, Batman!

It did include the stuff that was meant to go here, though, which
I hereby cut to.

Sorry about that.

Darn, and that was so much more interesting then GETs and POSTs :).
For a while you had me searching through the list to figure out what
YAEPT is. (Yet Another English Phonetis Thread ? )
:slight_smile:


Hal Fulton
hal9000@hypermetrics.com

Me thinks that CGI should always parse the url regardless of the
METHOD, seed the params with those values, then, if it is a POST, add
to the params. This should not be a separate call.

Dan

···

On Monday, Jul 21, 2003, at 01:19 America/New_York, Hal E. Fulton wrote:

----- Original Message -----
From: “Damphyr” damphyr@freemail.gr
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Sunday, July 20, 2003 4:55 PM
Subject: Re: Parsing POST and GET variables simultaneously?

Mark J. Reed wrote:

Eep! Holy Wrong Newsgroup, Batman!

It did include the stuff that was meant to go here, though, which
I hereby cut to.

Sorry about that.

Darn, and that was so much more interesting then GETs and POSTs :).
For a while you had me searching through the list to figure out what
YAEPT is. (Yet Another English Phonetis Thread ? )
:slight_smile:

Yeah, I thought it was interesting, too… meant for
sci.lang or where? Haven’t read that one in ages…

Hal


Hal Fulton
hal9000@hypermetrics.com


Dan Janowski
danj@3skel.com