tags:

views:

59

answers:

2
+1  Q: 

Ruby HTTP Request

Ruby

req = Net::HTTP.get_response(URI.parse('http://www.domain.com/coupons.txt'))

@play = req.body

req.body give me the entire page in to a string. What if I just want to read line by line? gets? Can you read line by line through a http get? Or do I just need to read a file from local?

The text file looks like this

1 John Ham 25,000

2 Ham John 25,000

3 Ohail Spam 25,000

4 Ted Dome 25,000

5 Di Di 25,000
A: 

Since the body method returns a string, I would have to assume you can use the String#each_line method. Check out the documentation for String#each_line.

Brian
Please be aware that this will still read the entire body from the socket.
Bob Aman
how do you avoid reading the entire body?
rogerdpack
+1  A: 

If you don't want to read the entire body, try using the Net::HTTPResponse#read_body method:

The body is provided in fragments, as it is read in from the socket.

Yaser Sulaiman