Query string ignored in http.post?

L

Lloyd Zusman

I'm using Net::HTTP to do a POST operation, but the query string I send
with variable settings seems to be ignored. Can anyone tell me what I
might be doing wrong?

require "net/http"

Net::HTTP.version_1_2
Net::HTTP.start('www.myhost.tld', 80) {
|http|
response = http.post('/test/showvars.php', 'foo=bar&ok=quack')
puts response.body()
}

When I run this, none of the variables show up as having been set.

However, if I send these variables to the script via a POST operation by
submitting them from a web page inside of a form like the one below,
I indeed see them as having been set:

<form action="http://www.myhost.tld/test/showvars.php" method="post">
<input type="hidden" name="foo" value="bar">
<input type="hidden" name="ok" value="quack">
<input type="submit" name="Submit" value="Submit">
</form>

Also, this has nothing to do with the /test/showvars.php script itself,
as I see the same results with any and all CGI's and servlets that I
invoke via http.post operations.

If I use the 1.1 version, I have the same problem with the POST:

require "net/http"

Net::HTTP.version_1_1
Net::HTTP.start('www.myhost.tld', 80) {
|http|
response, body = http.post('/test/showvars.php', 'foo=bar&ok=quack')
puts body
}

However, when I run this as a "GET", the variables get set fine:

require "net/http"

Net::HTTP.start('www.myhost.tld', 80) {
|http|
response = http.get('/test/showvars.php?foo=bar&ok=quack')
puts response.body()
}

I assume that I must be doing something wrong with the http.post
operation, but I can't figure out what that might be, since what I have
done seems to follow the Net::HTTP documentation.

Any ideas?

Oh, I almost forgot this:

% ruby --version
ruby 1.9.0 (2004-08-03) [i386-freebsd4.0]
% uname -sr
FreeBSD 4.0-STABLE

... and here's the showvars.php script:

<?php
$result = '';
if (isset($HTTP_POST_VARS)) {
$result .= "HTTP_POST_VARS:\n";
while (list($key, $val) = each($HTTP_POST_VARS)) {
$result .= " $key=$val\n";
}
}
if (isset($HTTP_GET_VARS)) {
$result .= "HTTP_GET_VARS:\n";
while (list($key, $val) = each($HTTP_GET_VARS)) {
$result .= " $key=$val\n";
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<title>Show variables</title>
</head>
<body>
<b><pre>
<?php echo $result ?>
</pre></b>
</body>
</html>


Thanks in advance.
 
P

Patrick May

Lloyd,

I'm using Net::HTTP to do a POST operation, but the query string I send
with variable settings seems to be ignored. Can anyone tell me what I
might be doing wrong?

require "net/http"

Net::HTTP.version_1_2
Net::HTTP.start('www.myhost.tld', 80) {
|http|
response = http.post('/test/showvars.php', 'foo=bar&ok=quack')
puts response.body()
}

When I run this, none of the variables show up as having been set.

It could either be ruby not sending the get variables, or php ignoring
the get variables. Is this php var being set appropriately:

$_SERVER['QUERY_STRING']

If this is always being set, then this is just a php thing. If this
variable isn't being set, then it's probably net/http.

just guessing,

patrick
 
L

Lloyd Zusman

Lloyd Zusman said:
I'm using Net::HTTP to do a POST operation, but the query string I send
with variable settings seems to be ignored. Can anyone tell me what I
might be doing wrong?

require "net/http"

Net::HTTP.version_1_2
Net::HTTP.start('www.myhost.tld', 80) {
|http|
response = http.post('/test/showvars.php', 'foo=bar&ok=quack')
puts response.body()
}

I figured out the answer. I need to explicitly set the Content-Type to
"application/x-www-form-urlencoded":

require "net/http"

Net::HTTP.version_1_2
Net::HTTP.start('www.myhost.tld', 80) {
|http|
response =
http.post('/test/showvars.php', 'foo=bar&ok=quack',
{ 'Content-Type' => 'application/x-www-form-urlencoded' })
puts response.body()
}

But shouldn't net/http do this for me when I'm calling the #post method
with a query string? The documentation for Net::HTTP seems to imply
this, as it doesn't specify an explicit setting of Content-Type to
"application/x-www-form-urlencoded" in the example that it shows.

From the Net::HTTP documentation:

Posting Form Data

require 'net/http'
Net::HTTP.start('some.www.server', 80) { |http|
response = http.post('/cgi-bin/search.rb', 'query=ruby')
}
 
L

Lloyd Zusman

Patrick May said:
Lloyd,

I'm using Net::HTTP to do a POST operation, but the query string I send
with variable settings seems to be ignored. Can anyone tell me what I
might be doing wrong?

require "net/http"

Net::HTTP.version_1_2
Net::HTTP.start('www.myhost.tld', 80) {
|http|
response = http.post('/test/showvars.php', 'foo=bar&ok=quack')
puts response.body()
}

When I run this, none of the variables show up as having been set.

It could either be ruby not sending the get variables, or php ignoring
the get variables. Is this php var being set appropriately:

$_SERVER['QUERY_STRING']

If this is always being set, then this is just a php thing. If this
variable isn't being set, then it's probably net/http.

Thank you.

As you now know from my follow-up to my own post, I solved the problem
by explicitly setting Content-Type to application/x-www-form-urlencoded.

This has to do with net/http, because as I mentioned, this occurs in all
CGI's and servlets, not just in the php example that I gave here. And
no, QUERY_STRING is not being set. This is also true for all CGI's and
servlets, not just php programs.

Furthermore, when I do the POST via the HTML <form ...> stuff that I
showed in my original message, the same php and all other CGI's and
servlets see QUERY_STRING being set, and the variables are available.
This no doubt causes the Content-Type to be properly set.
 
P

Patrick May

Hello,

As you now know from my follow-up to my own post, I solved the problem
by explicitly setting Content-Type to
application/x-www-form-urlencoded.

This has to do with net/http, because as I mentioned, this occurs in
all
CGI's and servlets, not just in the php example that I gave here. And
no, QUERY_STRING is not being set. This is also true for all CGI's and
servlets, not just php programs.

Furthermore, when I do the POST via the HTML <form ...> stuff that I
showed in my original message, the same php and all other CGI's and
servlets see QUERY_STRING being set, and the variables are available.
This no doubt causes the Content-Type to be properly set.

Makes sense. Glad you figured it out, thanks for posting the fix. I
was curious :)

Cheers,

patrick
 
L

Lloyd Zusman

Patrick May said:
Hello,

As you now know from my follow-up to my own post, I solved the problem
by explicitly setting Content-Type to
application/x-www-form-urlencoded.

[ ... ]

Makes sense. Glad you figured it out, thanks for posting the fix. I
was curious :)

Cheers,

patrick

My sincere thanks for your interest and support.

To the entire group: I'm still wondering why Net::HTTP#post does not
automatically set Content-Type to application/x-www-form-urlencoded.

There definitely is a precedent for it doing so, as a number of
utilities that do HTTP post's set the Content-Type in this manner as the
default behavior, often with an option to turn that behavior off in the
less likely case where this setting is not desired.

But if it's deemed desirable not to set this value by default, at the
very least I would think that the post example shown in the Net::HTTP
documentation should show this Content-Type value being set.

Thoughts?
 
M

Michael Fuhr

Lloyd Zusman said:
To the entire group: I'm still wondering why Net::HTTP#post does not
automatically set Content-Type to application/x-www-form-urlencoded.

Under ruby 1.8.2 (2004-11-08) it does, but if you run with -w (or
$VERBOSE=true) then you get the following warning:

net/http: warning: Content-Type did not set; using application/x-www-form-urlencoded

Under ruby 1.9.0 (2004-11-09) it neither sets Content-Type nor
prints the warning.
 
L

Lloyd Zusman

Under ruby 1.8.2 (2004-11-08) it does, but if you run with -w (or
$VERBOSE=true) then you get the following warning:

net/http: warning: Content-Type did not set; using
application/x-www-form-urlencoded

Under ruby 1.9.0 (2004-11-09) it neither sets Content-Type nor
prints the warning.

Hmm ... so it looks like that feature was removed in the later version
of the code. If so, does anyone know why? The 1.8.2 behavior seems
quite sensible to me.
 

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

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top