1

I want to fetch some content from a webserver using Net:HTTP, like this:

url = URI.parse('http://www.example.com/index.html')
res = Net::HTTP.start(url.host, url.port) {|http|
  http.get('/index.html')
}
puts res.body

But I need to limit the get to the first 5kb to reduce the network traffic. How do I do this?

bitboxer
  • 544
  • 6
  • 17
  • Correct answer: http://stackoverflow.com/questions/1120350/how-to-download-via-http-only-piece-of-big-file-with-ruby – inket Apr 08 '13 at 01:15

1 Answers1

1

I'm not sure when using Net::HTTP but using OpenURI i usually do the following:

require 'open-uri'

resource = open('http://google.com')

resource.read( 5120 ) 
=> # reads first 5120 characters, which i'm assuming would be 5KB.

hope this helps.

ucron
  • 2,822
  • 2
  • 18
  • 6
  • 2
    open-uri's open(url) fetches everything. This is a wrong answer considering his goal is reducing network traffic. – inket Apr 08 '13 at 00:44