Parsing POST and GET variables simultaneously?

  • Thread starter David Heinemeier Hansson
  • Start date
D

David Heinemeier Hansson

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 "<html><body>"
puts "<form action='test.rbx?valueFromGet=here' method='post'>"
puts "<input type='text' name='valueFromPost'>"
puts "</form></body></html>"
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?
 
J

james_b

David said:
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
 
A

Aredridel

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.
 
M

Mark J. Reed

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

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
 
M

Mark J. Reed

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 (e-mail address removed) Sun Jul 20 17:14:14 2003
From: "Mark J. Reed" <[email protected]>
To: David Heinemeier Hansson <[email protected]>
Subject: Re: Parsing POST and GET variables simultaneously?
References: <[email protected]> <[email protected]>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <[email protected]>
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: <[email protected]>
X-Mutt-Fcc: =sent
Status: RO
Content-Length: 943
Lines: 37

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 :)

May your wish come true. :) 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
 
M

Mark J. Reed

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 :)

May your wish come true. :) 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
 
D

Damphyr

Mark said:
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 ? )
:)
V.-




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

Mauricio Fernández

Eep! Holy Wrong Newsgroup, Batman!

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


--
_ _
| |__ __ _| |_ ___ _ __ ___ __ _ _ __
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
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
 
H

Hal E. Fulton

----- Original Message -----
From: "Damphyr" <[email protected]>
To: "ruby-talk ML" <[email protected]>
Sent: Sunday, July 20, 2003 4:55 PM
Subject: Re: Parsing POST and GET variables simultaneously?

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 ? )
:)

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

Hal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top