Net/http broken?

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?

tsuraan wrote:

···

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?

tsuraan wrote:

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

tsuraan wrote:

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

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

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

I'll give that a shot. Thanks!

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

James Britt wrote:

tsuraan wrote:

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.

>

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

The docs on ruby-doc.org are taken from the CVS head. The documentation for net/http (which I believe I was the last person to update) reflects the version of net/http in CVS at the time, which is the version that became part of the Ruby 1.8.4 release.

So the docs are (probably)(*) right, you just need to either use a more recent Ruby, or use an older version of the docs.

mathew
[ (*) I say "probably" because I didn't check the specific example code
   for HTTP post already given in the sources; I just checked the code
   examples I added. ]

···

--
      <URL:http://www.pobox.com/~meta/>
My parents went to the lost kingdom of Hyrule
     and all I got was this lousy triforce.

Oh my god this is hilarious! I just had to deal with this exact problem!

It's definitely a 1.8.2/1.8.3 thing... that post_form method doesn't exist
in 1.8.2.

Take care,

···

On 11/11/05, mathew <meta@pobox.com> wrote:

James Britt wrote:
>tsuraan wrote:
>> 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.
>
> Seems the docs are wrong. I had the same problem not too long ago.

The docs on ruby-doc.org <http://ruby-doc.org> are taken from the CVS
head. The documentation
for net/http (which I believe I was the last person to update) reflects
the version of net/http in CVS at the time, which is the version that
became part of the Ruby 1.8.4 release.

So the docs are (probably)(*) right, you just need to either use a more
recent Ruby, or use an older version of the docs.

mathew
[ (*) I say "probably" because I didn't check the specific example code
for HTTP post already given in the sources; I just checked the code
examples I added. ]
--
<URL:http://www.pobox.com/~meta/>
My parents went to the lost kingdom of Hyrule
and all I got was this lousy triforce.

--
-

Christian Leskowsky
christian.leskowsky@gmail.com

mathew wrote:

[...] you just need to either use a more recent Ruby, or [...]

I'd be tempted to just scoop http.rb, https.rb & protocol.rb out of:
http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/ruby/lib/net/?only_with_tag=v1_8_3
into a new directory and prepend to $:

daz