net/http broken?

T

tsuraan

The documentation for net/http on ruby-doc.org gives the following
snippet for getting data from a server using HTTP POST:

require 'net/http'
require 'uri'

res =3D Net::HTTP.post_form(URI.parse('http://www.example.com/search.cg=
i'),
{'q'=3D>'ruby', 'max'=3D>'50'})
puts res.body

The problem with this is that in ruby 1.8.2 on both my OSX box and my
FreeBSD computer, Net::HTTP does not have a post_form defined. So,
that example doesn't work. Another way of doing the same idea, given
by ruby-doc.org, is:

url =3D URI.parse('http://www.example.com/todo.cgi')
req =3D Net::HTTP::post.new(url.path)
req.set_form_data({'from'=3D>'2005-01-01', 'to'=3D>'2005-03-31'}, ';')
res =3D Net::HTTP.new(url.host, url.port).start { http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
# OK
else
res.error!
end

This has the problem that req has no set_form_data method. So, does
anyone know what's going wrong? Is it possible to use the ruby
standard libraries to post data to a server?
 
R

Robert Klemme

tsuraan said:
The documentation for net/http on ruby-doc.org gives the following
snippet for getting data from a server using HTTP POST:

require 'net/http'
require 'uri'

res =


Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'),
{'q'=>'ruby', 'max'=>'50'}) puts res.body

The problem with this is that in ruby 1.8.2 on both my OSX box and my
FreeBSD computer, Net::HTTP does not have a post_form defined. So,
that example doesn't work. Another way of doing the same idea, given
by ruby-doc.org, is:

url = URI.parse('http://www.example.com/todo.cgi')
req = Net::HTTP::post.new(url.path)
req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';')
res = Net::HTTP.new(url.host, url.port).start { http.request(req)
} case res
when Net::HTTPSuccess, Net::HTTPRedirection
# OK
else
res.error!
end

This has the problem that req has no set_form_data method. So, does
anyone know what's going wrong? Is it possible to use the ruby
standard libraries to post data to a server?
 
R

Robert Klemme

tsuraan said:
The documentation for net/http on ruby-doc.org gives the following
snippet for getting data from a server using HTTP POST:

require 'net/http'
require 'uri'

res =


Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'),
{'q'=>'ruby', 'max'=>'50'}) puts res.body

The problem with this is that in ruby 1.8.2 on both my OSX box and my
FreeBSD computer, Net::HTTP does not have a post_form defined.

Hm, 1.8.3 has it:

17:02:52 [~]: ruby -r net/http -e 'p RUBY_VERSION; p
Net::HTTP.public_methods.grep /post/i'
-e:1: warning: parenthesize argument(s) for future version
"1.8.3"
["post_form"]

This is on cygwin. I wouldn't assume that post_form was removed in 1.8.2
and added in 1.8.3. Are you sure the method is not present? (You can
test with the same code snippet.)

Kind regards

robert
 
J

James Britt

tsuraan said:
The documentation for net/http on ruby-doc.org gives the following
snippet for getting data from a server using HTTP POST:

require 'net/http'
require 'uri'

res = Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'),
{'q'=>'ruby', 'max'=>'50'})
puts res.body

The problem with this is that in ruby 1.8.2 on both my OSX box and my
FreeBSD computer, Net::HTTP does not have a post_form defined. So,
that example doesn't work. Another way of doing the same idea, given
by ruby-doc.org, is:

url = URI.parse('http://www.example.com/todo.cgi')
req = Net::HTTP::post.new(url.path)
req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';')
res = Net::HTTP.new(url.host, url.port).start { http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
# OK
else
res.error!
end

Seems the docs are wrong. I had the same problem not too long ago.

Here's an example of what works for me:

require 'net/http'

# post some data to uri:port/page

uri = 'www.example.com'
port = 80
page = '/RPC/foo'
data = '<foo><bar>Doodle!</bar></foo>'

Net::HTTP.start( uri, port) { |http|
results = http.post( page, data )
}


James

--

http://www.ruby-doc.org - Ruby Help & Documentation
http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys
http://www.30secondrule.com - Building Better Tools
 
T

tsuraan

Hm, 1.8.3 has it:
17:02:52 [~]: ruby -r net/http -e 'p RUBY_VERSION; p
Net::HTTP.public_methods.grep /post/i'
-e:1: warning: parenthesize argument(s) for future version
"1.8.3"
["post_form"]

Hm, I get just "1.8.2" when I run that. Same thing on my apple and my
freebsd box. Strange...
 
T

tsuraan

Seems the docs are wrong. I had the same problem not too long ago.
Here's an example of what works for me:

require 'net/http'

# post some data to uri:port/page

uri =3D 'www.example.com'
port =3D 80
page =3D '/RPC/foo'
data =3D '<foo><bar>Doodle!</bar></foo>'

Net::HTTP.start( uri, port) { |http|
results =3D http.post( page, data )
}

I'll give that a shot. Thanks!
 
J

James Britt

Robert Klemme wrote:
...
Hm, 1.8.3 has it:

17:02:52 [~]: ruby -r net/http -e 'p RUBY_VERSION; p
Net::HTTP.public_methods.grep /post/i'
-e:1: warning: parenthesize argument(s) for future version
"1.8.3"
["post_form"]

This is on cygwin. I wouldn't assume that post_form was removed in 1.8.2
and added in 1.8.3. Are you sure the method is not present? (You can
test with the same code snippet.)

Ah. That explains my problem using the Windows installer version.
There has been no 1.8.3 release, for assorted reasons, and I forgot that
I'm therefore not running the latest Ruby.

So my suggestion is a 1.8.2 bit of code.

I believe Curt will release a 1.8.4 Windows installer version when Matz
releases that version of Ruby around Christmas.

James

--

http://www.ruby-doc.org - Ruby Help & Documentation
http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys
http://www.30secondrule.com - Building Better Tools
 

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,968
Messages
2,570,150
Members
46,696
Latest member
BarbraOLog

Latest Threads

Top