# Posting to an http server using pre-1.8.2 Ruby

**URL:** <https://www.ruby-forum.com/t/posting-to-an-http-server-using-pre-1-8-2-ruby/81506>\
**Category:** Ruby\
**Created:** [March 13, 2006, 11:38pm UTC](https://www.ruby-forum.com/t/posting-to-an-http-server-using-pre-1-8-2-ruby/81506 "2006-03-13T23:38:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dandante](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@dandante](https://www.ruby-forum.com/u/dandante)\
**Post date:** [March 13, 2006, 11:38pm UTC](https://www.ruby-forum.com/t/posting-to-an-http-server-using-pre-1-8-2-ruby/81506/1 "2006-03-13T23:38:54Z")

</div>

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 method`strip’ 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:in`initialize’  
from /usr/lib/ruby/1.8/net/http.rb:1181:in `initialize' from foo.rb:4:in`new’  
from foo.rb:4  
from foo.rb:3:in `start' from /usr/lib/ruby/1.8/net/http.rb:324:in`start’  
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.
