I tried the following example code:
require ‘net/http’
h = Net::HTTP.new(‘www.pragmaticprogrammer.com’, 80)
resp, data = h.get(’/index.html’, nil)
puts "Code = #{resp.code}"
puts "Message = #{resp.message}"
resp.each {|val, key| printf “%-14s = %-40.40s\n”, key, val }
p data[0…55]
This ran as expected under ruby 1.6.7 (2002-03-01) [powerpc-darwin6.0]
but when I ran it under ruby 1.8.0 (2003-05-26) [powerpc-darwin6.6] the
data variable was nil, resulting in a “net.rb:8: undefined method `[]'
for nil (NoMethodError)”. resp was the same under both versions however.
What can I do to narrow this problem down?
···
Urban Nilsson, http://bonk.nu/blog
’What we in the West call “bugs”, the Japanese call “spoilage”. I find
this nomenclature honest and refreshing. “Bug” implies that the
problem is some independent agent, when in fact the problem is the
"spoiled" code itself.’ – Handy Vandal
After:
resp, data = h.get(‘/index.html’, nil)
try adding this line:
data = resp.data if data.nil?
That should make your code compatible with both versions of net/http.
Ian
···
On Fri 06 Jun 2003 at 02:54:03 +0900, Urban Nilsson wrote:
I tried the following example code:
require ‘net/http’
h = Net::HTTP.new(‘www.pragmaticprogrammer.com’, 80)
resp, data = h.get(‘/index.html’, nil)
puts “Code = #{resp.code}”
puts “Message = #{resp.message}”
resp.each {|val, key| printf “%-14s = %-40.40s\n”, key, val }
p data[0…55]
This ran as expected under ruby 1.6.7 (2002-03-01) [powerpc-darwin6.0]
but when I ran it under ruby 1.8.0 (2003-05-26) [powerpc-darwin6.6] the
data variable was nil, resulting in a “net.rb:8: undefined method `’
for nil (NoMethodError)”. resp was the same under both versions however.
What can I do to narrow this problem down?
–
Ian Macdonald | From the cradle to the coffin underwear
System Administrator | comes first. – Bertolt Brecht
ian@caliban.org |
http://www.caliban.org |
>
‘What we in the West call “bugs”, the Japanese call “spoilage”. I find
this nomenclature honest and refreshing. “Bug” |implies that the
problem is some independent agent, when in fact the problem is the
“spoiled” code itself.’ – Handy |Vandal
…I would call it …‘humans’
Urban Nilsson wrote:
···
I tried the following example code:
require ‘net/http’
h = Net::HTTP.new(‘www.pragmaticprogrammer.com’, 80)
resp, data = h.get(‘/index.html’, nil)
puts “Code = #{resp.code}”
puts “Message = #{resp.message}”
resp.each {|val, key| printf “%-14s = %-40.40s\n”, key, val }
p data[0…55]
This ran as expected under ruby 1.6.7 (2002-03-01) [powerpc-darwin6.0]
but when I ran it under ruby 1.8.0 (2003-05-26) [powerpc-darwin6.6]
the data variable was nil, resulting in a “net.rb:8: undefined method
`’ for nil (NoMethodError)”. resp was the same under both versions
however.
What can I do to narrow this problem down?
Urban Nilsson, http://bonk.nu/blog
‘What we in the West call “bugs”, the Japanese call “spoilage”. I
find this nomenclature honest and refreshing. “Bug” implies that the
problem is some independent agent, when in fact the problem is the
“spoiled” code itself.’ – Handy Vandal
I tried the following example code:
require ‘net/http’
h = Net::HTTP.new(‘www.pragmaticprogrammer.com’, 80)
resp, data = h.get(‘/index.html’, nil)
puts “Code = #{resp.code}”
puts “Message = #{resp.message}”
resp.each {|val, key| printf “%-14s = %-40.40s\n”, key, val }
p data[0…55]
This ran as expected under ruby 1.6.7 (2002-03-01) [powerpc-darwin6.0]
but when I ran it under ruby 1.8.0 (2003-05-26) [powerpc-darwin6.6] the
data variable was nil, resulting in a “net.rb:8: undefined method `’
for nil (NoMethodError)”. resp was the same under both versions however.
What can I do to narrow this problem down?
It’s a change in the interface, breaking old code.
This bit me a couple of weeks ago, and Austin Ziegler
pointed it out to me:
For VERSION =~ /1.[789]/, Net::HTTP#get has been changed:
resp = h.get(“/”, nil)
data = resp.body
More logical, perhaps, but incompatible.
Hal
···
----- Original Message -----
From: “Urban Nilsson” urban@oops.se
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, June 05, 2003 12:54 PM
Subject: Ruby 1.8.0 net/http bug
Urban Nilsson wrote:
I tried the following example code:
require ‘net/http’
h = Net::HTTP.new(‘www.pragmaticprogrammer.com’, 80)
resp, data = h.get(‘/index.html’, nil)
puts “Code = #{resp.code}”
puts “Message = #{resp.message}”
resp.each {|val, key| printf “%-14s = %-40.40s\n”, key, val }
p data[0…55]
net::HTTP in 1.8 is (unfortunately) incompatible with previous versions.
It now returns a single object, with the response contents accessible
via the method 'body.
try
resp = h.get(...)
p resp.body[0..55]
Cheers
Dave
Hal E. Fulton wrote:
resp, data = h.get(‘/index.html’, nil)
try adding this line:
data = resp.data if data.nil?
That should make your code compatible with both versions of net/http.
Ah, that’s a good idea.
Even better perhaps would be
class Net::HTTPOK
def to_ary
return self, body
end
end
That way all original code should still work unchanged.
Is this worth an RCR?
Cheers
Dave
I was just typing up the same code when I saw your mail come in. I
believe it is worth an RCR.
Chad
···
On Fri, 6 Jun 2003, Dave Thomas wrote:
Hal E. Fulton wrote:
resp, data = h.get(‘/index.html’, nil)
try adding this line:
data = resp.data if data.nil?
That should make your code compatible with both versions of net/http.
Ah, that’s a good idea.
Even better perhaps would be
class Net::HTTPOK
def to_ary
return self, body
end
end
That way all original code should still work unchanged.
Is this worth an RCR?
Hi,
In mail “Re: Ruby 1.8.0 net/http bug”
resp, data = h.get(‘/index.html’, nil)
try adding this line:
data = resp.data if data.nil?
Even better perhaps would be
class Net::HTTPOK
def to_ary
return self, body
end
end
That way all original code should still work unchanged.
But it is not only change.
In 1.8, Net::HTTP#get does not raises exceptions on 4xx/5xx.
Inserting Net::HTTP.version_1_1 declaration is not enough?
e.g.
require ‘net/http’
Net::HTTP.version_1_1 # forces ruby 1.6 behavior
Regards,
Minero Aoki
···
Dave Thomas dave@pragprog.com wrote:
Thanks for the help everybody!
/U
torsdagen den 5 juni 2003 kl 20.35 skrev Dave Thomas:
···
Hal E. Fulton wrote:
resp, data = h.get(‘/index.html’, nil)
try adding this line:
data = resp.data if data.nil?
That should make your code compatible with both versions of net/http.
Ah, that’s a good idea.
Even better perhaps would be
class Net::HTTPOK
def to_ary
return self, body
end
end
That way all original code should still work unchanged.
Is this worth an RCR?
Cheers
Dave
Urban Nilsson
‘What we in the West call “bugs”, the Japanese call “spoilage”. I find
this nomenclature honest and refreshing. “Bug” implies that the
problem is some independent agent, when in fact the problem is the
“spoiled” code itself.’ – Handy Vandal
Minero Aoki wrote:
But it is not only change.
In 1.8, Net::HTTP#get does not raises exceptions on 4xx/5xx.
Inserting Net::HTTP.version_1_1 declaration is not enough?
e.g.
require ‘net/http’
Net::HTTP.version_1_1 # forces ruby 1.6 behavior
True, but adding a to_ary will fix the most common problems, and is
compatible with both the old and new behaviours. It also doesn’t require
a change to every program out there that currently uses net::HTTP.
Regards
Dave
Hi,
In mail “Re: Ruby 1.8.0 net/http bug”
···
Dave Thomas dave@pragprog.com wrote:
But it is not only change.
In 1.8, Net::HTTP#get does not raises exceptions on 4xx/5xx.
Inserting Net::HTTP.version_1_1 declaration is not enough?
True, but adding a to_ary will fix the most common problems, and is
compatible with both the old and new behaviours. It also doesn’t require
a change to every program out there that currently uses net::HTTP.
OK, I have added Net::HTTPResponse#to_ary.
Regards,
Minero Aoki