Net::HTTP/setup issue

221 views
Skip to first unread message

rheoled

unread,
Jan 27, 2012, 11:50:40 AM1/27/12
to RubyInstaller
I'm running into a problem with a new Windows 7 Ruby 1.9.3
installation, getting this error:

C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1019:in `get': undefined method
`keys' for nil:NilClass (NoMethodError)

Any clues?


Here's what I'm seeing when I run my test script:

====
$ ruby -rubygems test.rb
C:/Ruby193/lib/ruby/gems/1.9.1/gems/windows-api-0.4.0/lib/windows/
api.rb:4: Use RbConfig instead of obsolete and deprecated Config.
$ ruby -rubygems test.rb
C:/Ruby193/lib/ruby/gems/1.9.1/gems/windows-api-0.4.0/lib/windows/
api.rb:4: Use RbConfig instead of obsolete and deprecated Config.
C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1019:in `get': undefined method
`keys' for nil:NilClass (NoMethodError)
from test.rb:237:in `block in op_elap'
from C:/Ruby193/lib/ruby/1.9.1/timeout.rb:68:in `timeout'
from C:/Ruby193/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
from test.rb:228:in `op_elap'
from test.rb:47:in `poll'
from test.rb:284:in `<main>'
2012-01-27 09:47:00 (ET: 0.000s): Polling ...
2012-01-27 09:47:00 (ET: 0.000s): app_page:
http://10.1.66.66/login.html?username=sitemetrics&password=sitemetrics1
2012-01-27 09:47:00 (ET: 0.000s): Calling Water::IE.new_process ...
2012-01-27 09:47:02 (ET: 1.454s): Setting ie speed to fast ...
2012-01-27 09:47:02 (ET: 0.000s): Starting LOGON to
http://10.1.66.66/login.html?username=sitemetrics&password=sitemetrics1
2012-01-27 09:47:02 (ET: 0.000s): Calling op_elap for LOGON
2012-01-27 09:47:02 (ET: 0.000s): Entered op_elap(opname=LOGON,
ip=10.1.66.66
2012-01-27 09:47:02 (ET: 0.000s): Launching no-op HTTP request to
10.1.66.66:80 for LOGON

====

Here's the code fragment that's triggering the error:

====
if ip and port
logmsg "Launching no-op HTTP request to " + ip + ":" +
port.to_s + " for " + opname
Net::HTTP.new(ip, port).get("WATIR?op="+opname, nil)
logmsg "Returned from no-op HTTP request for " + opname
end
====

Here's the ruby doc for Net::HTTP::get:

====
get(uri_or_host, path = nil, port = nil) click to toggle source

Sends a GET request to the target and returns the HTTP response as a
string. The target can either be specified as (uri), or as (host,
path, port = 80); so:

print Net::HTTP.get(URI('http://www.example.com/index.html'))

or:

print Net::HTTP.get('www.example.com', '/index.html')
====

I see my 1.9.3 install of Ruby gave me a 1.9.1 path component to
Net::HTTP code, is that an issue??

What am I missing here?

Thanks in advance for any help!

Ed

Luis Lavena

unread,
Feb 6, 2012, 5:53:39 PM2/6/12
to rubyin...@googlegroups.com
On Fri, Jan 27, 2012 at 1:50 PM, rheoled <efl...@gmail.com> wrote:
> I'm running into a problem with a new Windows 7 Ruby 1.9.3
> installation, getting this error:
>
> C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1019:in `get': undefined method
> `keys' for nil:NilClass (NoMethodError)
>
> Any clues?
>

Your script is not complete, please provide a complete script that we
can reproduce.

>
> I see my 1.9.3 install of Ruby gave me a 1.9.1 path component to
> Net::HTTP code, is that an issue??
>

1.9.1 is API version compatibility, Ruby 1.9.3 is API compatible with
Ruby 1.9.1 and 1.9.2, is not Ruby version (the one you get when using
-v)

> What am I missing here?
>

Please provide a simple script that recreates the issue and we will be
able to help you, from skimming your snippets nothing seems wrong, so
there must be something you're missing.

The best is try to reproduce it with the minimum detail.

PS: Sorry took so long to notice your message, Google Groups didn't
email us (admins) about it.
--
Luis Lavena
AREA 17
-
Perfection in design is achieved not when there is nothing more to add,
but rather when there is nothing more to take away.
Antoine de Saint-Exupéry

Tad

unread,
Feb 28, 2012, 6:06:16 PM2/28/12
to rubyin...@googlegroups.com
Hi 
I'm getting the same problem while trying a piece of example code from  http://www.rubycentral.com/pickaxe/tut_threads.html
I'm running 1.9.3p0. I think it is a bug in net/http, but there must be a workaround or a fix by now ?

The code:
require 'net/http'
threads = []
for page in pages
  threads << Thread.new(page) { |myPage|
    h = Net::HTTP.new(myPage, 80)
    puts "Fetching: #{myPage}"
    resp, data = h.get('/', nil )
    puts "Got #{myPage}:  #{resp.message}"
  }
end
threads.each { |aThread|  aThread.join }
Result:
Fetching: www.awl.com
C:/Documents and Settings/Tad/.pik/rubies/Ruby-193-p125/lib/ruby/1.9.1/net/http.rb:1019:in `get': undefined method `keys' for nil:NilClass (NoMethodError)
        from threads.rb:14:in `block (2 levels) in <main>'

==========================================================

Luis Lavena

unread,
Feb 28, 2012, 6:16:42 PM2/28/12
to rubyin...@googlegroups.com
On Tue, Feb 28, 2012 at 8:06 PM, Tad <tad.b...@gmail.com> wrote:
> Hi
> I'm getting the same problem while trying a piece of example code from
>  http://www.rubycentral.com/pickaxe/tut_threads.html
> I'm running 1.9.3p0. I think it is a bug in net/http, but there must be a
> workaround or a fix by now ?
>

The problem is your code:

http://rubydoc.info/stdlib/net/1.9.2/Net/HTTP:get

You're sending "nil" to an expected Hash (header options)

If you remove "nil" from get it will work:

V:\>ruby t.rb

Got www.rubycentral.com: OK
Got www.pragmaticprogrammer.com: Found
Got www.awl.com: Moved Temporarily

Tad

unread,
Feb 28, 2012, 6:45:15 PM2/28/12
to RubyInstaller
Ok.
I don't understand all the stuff in the bowels, but my workaround is a
patch to http.rb :
It seems that the parameter defaulting doesn't work like it used to,
so its probably a 1.9.3 problem rather than http.rb

def get(path, initheader = {}, dest = nil, &block) # :yield:
+body_segment+
res = nil
if HAVE_ZLIB
initheader={} unless initheader # MYPATCH
unless initheader.keys.any?{|k| k.downcase == "accept-
encoding"}
initheader = initheader.merge({
"accept-encoding" =>
"gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
})
@compression = true
end
end

On Feb 29, 12:06 am, Tad <tad.boc...@gmail.com> wrote:
> Hi
> I'm getting the same problem while trying a piece of example code from
>  http://www.rubycentral.com/pickaxe/tut_threads.html
> I'm running 1.9.3p0. I think it is a bug in net/http, but there must be a
> workaround or a fix by now ?
>
> The code:
> require 'net/http'
> pages = %w(www.rubycentral.comwww.awl.com www.pragmaticprogrammer.com)
> > 2012-01-27<http://10.1.66.66/login.html?username=sitemetrics&password=sitemetric...>09:47:00 (ET:  0.000s):  Calling Water::IE.new_process ...
> > 2012-01-27 09:47:02 (ET:  1.454s):  Setting ie speed to fast ...
> > 2012-01-27 09:47:02 (ET:  0.000s):  Starting LOGON to
> >http://10.1.66.66/login.html?username=sitemetrics&password=sitemetrics1
> > 2012-01-27<http://10.1.66.66/login.html?username=sitemetrics&password=sitemetric...>09:47:02 (ET:  0.000s):  Calling op_elap for LOGON

Luis Lavena

unread,
Feb 28, 2012, 7:05:47 PM2/28/12
to rubyin...@googlegroups.com
On Tue, Feb 28, 2012 at 8:45 PM, Tad <tad.b...@gmail.com> wrote:
> Ok.
> I don't understand all the stuff in the bowels, but my workaround is a
> patch to http.rb :
> It seems that the parameter defaulting doesn't work like it used to,
> so its probably a 1.9.3 problem rather than http.rb
>

Please don't do that...

I pointed you to the documentation where it states that the second
parameter of get method is a Hash, and you're sending a nil instead:

resp, data = h.get('/', nil )

Change that line to:

resp, data = h.get('/')

Solves the error.

There is no need to patch, simple to read the documentation. If you
don't want to send additional headers then don't send them, leave the
OPTIONAL argument out.

PS: In case the one-liner correction and the link to the documentation
aren't enough, your script, corrected, here:
https://gist.github.com/1936352

Bryan Bibat

unread,
Feb 28, 2012, 7:24:09 PM2/28/12
to rubyin...@googlegroups.com
To add to Luis' advice, take note that the book you're using refers to Ruby 1.6

http://www.rubycentral.com/pickaxe/preface.html

"This book documents Version 1.6 of Ruby, which was released in September 2000."

Please don't expect all of the code to work verbatim. But if you prefer copy-pasting code instead of simply looking up the updated API, you can use Ruby 1.8.7 instead of 1.9.3 to get less of these types of errors.


--
You received this message because you are subscribed to the Google Groups "RubyInstaller" group.
To post to this group, send email to rubyin...@googlegroups.com.
To unsubscribe from this group, send email to rubyinstalle...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyinstaller?hl=en.




--
Bryan Bibat
Freelance Software Engineer
http://www.bryanbibat.net

Tad

unread,
Feb 29, 2012, 5:13:10 PM2/29/12
to RubyInstaller
On Feb 29, 1:24 am, Bryan Bibat <b...@bryanbibat.net> wrote:
> To add to Luis' advice, take note that the book you're using refers to Ruby
> *1.6*
>
> http://www.rubycentral.com/pickaxe/preface.html
>
> "This book documents Version 1.6 of Ruby, which was released in September
> 2000."

Thank you very much Luis and Bryan. Your efforts are very much
appreciated.

I in fact was not 'reading' the 1.6 book, I just found the example on
Threads with Google, cut/paste and got the error.
I saw that Rheoled got the same error in a different context and so
assumed that it must have been a 1.9.3 problem.
And we know that Assumption is the Mother of all f*ps !
I never even considered that the error was in the example code from
Pickaxe !

Take care. Best regards.


Reply all
Reply to author
Forward
0 new messages