Posting to an http server using pre-1.8.2 Ruby

Hello,

How do I send data to an http server using POST and basic
authentication in Ruby 1.8.2?

I know the easy answer is, upgrade to the latest version of Ruby and use
Net::HTTP:post_form().

However, I am stuck with 1.8.2 because I am using Locomotive (a
self-contained Ruby/Rails environment for Mac OS X) and have been told
by the developers of Locomotive that it’s very difficult to upgrade
the version of Ruby that it uses, that I should wait for the new
release. But I can’t wait that long.

I tried extending Ruby 1.8.2 to implement that method but that’s
difficult as there are many other dependencies which are also new. It
will be much easier for me to just use pre-1.8.3 functionality to
accomplish my goal, and change the method calls later when the new
Locomotive is available.

The question is, how to do this simple thing. The following code works:

require ‘net/http’
Net::HTTP.start(‘localhost’,8080) {|http|
req = Net::HTTP::Post.new(’/admin/test.jsp’)
req.basic_auth ‘user’, ‘pass’
response = http.request(req)
print response.body
}

But it doesn’t post any data. If I try to pass some by changing the
third line to:
req = Net::HTTP::Post.new(’/admin/test.jsp’, ‘foo=bar’)

I get this:

/usr/lib/ruby/1.8/net/http.rb:1106:in initialize': undefined methodstrip’ for nil:NilClass (NoMethodError)
from /usr/lib/ruby/1.8/net/http.rb:1103:in each' from /usr/lib/ruby/1.8/net/http.rb:1103:ininitialize’
from /usr/lib/ruby/1.8/net/http.rb:1181:in initialize' from foo.rb:4:innew’
from foo.rb:4
from foo.rb:3:in start' from /usr/lib/ruby/1.8/net/http.rb:324:instart’
from foo.rb:3

The following code also works:

require ‘net/http’
Net::HTTP.start(‘localhost’, 8080) { |http|
response = http.post(’/test.jsp’, ‘query=ruby’)
print response.body
}

This time it is posting to a different file that is not protected by
basic authentication.
I know it works because the resulting page prints out all http
parameters.But I can’t get it to work with basic authentication.

The problem is, I can’t seem to combine the two tasks I need (that is,
basic auth, and posting form data). It has to be a post and not a get
request because of the amount of data involved.

I know I’m missing something simple…Hope someone can help.