Using net / http read_body Net::HTTPOK#read_body called twice (IOError)

Hi I'm trying to learn the net / http library and use it to write a simple
deployment script. I'm getting the following IOError, and not sure what's
wrong with the way I'm trying to use the library. Can somebody give me some
hints?

/usr/lib/ruby/2.2.0/net/http/response.rb:195:in `read_body':
Net::HTTPOK#read_body called twice (IOError)
from ./deploy_application.rb:38:in `get_file'
from ./deploy_application.rb:60:in `block in <main>'
from ./deploy_application.rb:59:in `each'
from ./deploy_application.rb:59:in `<main>'

def get_file(file)

  puts BASE_URI
  puts file
  uri=URI.join(BASE_URI,file)
  puts uri
  request=Net::HTTP::Get.new uri #.request_uri
  puts request
  response=@http.request request
  case response
    when Net::HTTPOK
      size = 0
      progress = 0
      total = response.header["Content-Length"].to_i
      $log.info("file found at #{uri}")
      puts "200"
      # need to handle file open error
      Dir.mkdir "/tmp/#{STAMP}"
      File.open "/tmp/#{STAMP}/#{file}",'wb' do |io|
        response.read_body do |chunk|
          size+=chunk.size
          new_progress = (size * 100) / total
          unless new_progress == progress
              log.info("\rDownloading %s (%3d%%) " % [file, new_progress])
          end
          progress = new_progress
          io.write chunk
        end
      end
    when 404
      $log.error("maven repository file #{uri} not found")
      exit 4
    when 500...600
      $log.error("error getting #{uri}, server returned #{response.code}")
      exit 5
    else
      $log.error("unknown http response code #{response.code}")
  end
end

@http=Net::HTTP.new(BASE_URI.host, BASE_URI.port)
files=["#{service}-#{version}.zip.md5","#{service}-#{version}.jar","#{service}-#{version}.jar.md5"].each
do |file| #"#{service}-#{version}.zip",
    get_file(file)
end

I think you should not read_body of response more than once; just read and
assign to a variable and then use it via this variable reference.
yazdı:

···

12 Nis 2015 ÖÖ 7:25 tarihinde "Colin Kincaid Williams" <discord@uw.edu>

Hi I'm trying to learn the net / http library and use it to write a simple
deployment script. I'm getting the following IOError, and not sure what's
wrong with the way I'm trying to use the library. Can somebody give me some
hints?

/usr/lib/ruby/2.2.0/net/http/response.rb:195:in `read_body':
Net::HTTPOK#read_body called twice (IOError)
from ./deploy_application.rb:38:in `get_file'
from ./deploy_application.rb:60:in `block in <main>'
from ./deploy_application.rb:59:in `each'
from ./deploy_application.rb:59:in `<main>'

def get_file(file)

  puts BASE_URI
  puts file
  uri=URI.join(BASE_URI,file)
  puts uri
  request=Net::HTTP::Get.new uri #.request_uri
  puts request
  response=@http.request request
  case response
    when Net::HTTPOK
      size = 0
      progress = 0
      total = response.header["Content-Length"].to_i
      $log.info("file found at #{uri}")
      puts "200"
      # need to handle file open error
      Dir.mkdir "/tmp/#{STAMP}"
      File.open "/tmp/#{STAMP}/#{file}",'wb' do |io|
        response.read_body do |chunk|
          size+=chunk.size
          new_progress = (size * 100) / total
          unless new_progress == progress
              log.info("\rDownloading %s (%3d%%) " % [file, new_progress])
          end
          progress = new_progress
          io.write chunk
        end
      end
    when 404
      $log.error("maven repository file #{uri} not found")
      exit 4
    when 500...600
      $log.error("error getting #{uri}, server returned #{response.code}")
      exit 5
    else
      $log.error("unknown http response code #{response.code}")
  end
end

@http=Net::HTTP.new(BASE_URI.host, BASE_URI.port)
files=["#{service}-#{version}.zip.md5","#{service}-#{version}.jar","#{service}-#{version}.jar.md5"].each
do |file| #"#{service}-#{version}.zip",
    get_file(file)
end