Forum: Ruby Reading an HTTPResponse line by line

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Scara Maccai (Guest)
on 2007-01-18 17:32
(Received via mailing list)
Hi,
how can I read a response from an http server line by line?

req = Net::HTTP.new('10.2.1.2')
res = req.post('/mycgi', mydata) do |str|
         print str
       end

How can I read a line instead of a "chunck" of data in str?
Something like "BufferedReader.readLine()" in Java...
unknown (Guest)
on 2007-01-18 17:46
(Received via mailing list)
On Fri, 19 Jan 2007, Scara Maccai wrote:

>
harp:~ > cat a.rb
   require 'stringio'

   sio = StringIO.new
   req = Net::HTTP.new '10.2.1.2'
   mydata = ''
   res = req.post('/mycgi', mydata){|str| sio << str}
   sio.rewind
   sio.each{|line| p line}

-a
Dale M. (Guest)
on 2007-01-18 17:51
(Received via mailing list)
Scara Maccai wrote:
> Hi,
> how can I read a response from an http server line by line?
>
> req = Net::HTTP.new('10.2.1.2')
> res = req.post('/mycgi', mydata) do |str|
>          print str
>        end
>
> How can I read a line instead of a "chunck" of data in str?
> Something like "BufferedReader.readLine()" in Java...

Do you mean something like:

require 'net/http'

h = Net::HTTP.new('www.ruby-lang.org', 80)
resp, data = h.get('/index.html', nil)

data.each_line do |line|
  puts "--> #{line}"
end
Scara Maccai (Guest)
on 2007-01-18 18:38
(Received via mailing list)
In every case (your way or the StringIO way) it looks to me that the
response is parsed at the very end, so I have to wait for the whole
response to be finished before starting using it: with
"BufferedReader.readLine()" I use the lines as soon as they are
available. Is there a way to do that with Ruby?



(thank you both for your answers)
unknown (Guest)
on 2007-01-18 19:00
(Received via mailing list)
On Fri, 19 Jan 2007, Scara Maccai wrote:

> In every case (your way or the StringIO way) it looks to me that the response
> is parsed at the very end, so I have to wait for the whole response to be
> finished before starting using it: with "BufferedReader.readLine()" I use the
> lines as soon as they are available. Is there a way to do that with Ruby?

just give the post method a block.  for instance, although google
doesn't
allow posting to this url

   harp:~ > cat a.rb
   require 'net/http'

   Net::HTTP.start 'fortytwo.merseine.nu', 80  do |http|
     http.post '/form.cgi', 'k=v&a=42' do |chunk|
       puts chunk
     end
   end


   harp:~ > ruby a.rb
   ---
   k:
   - v
   a:
   - "42"


-a
Scara Maccai (Guest)
on 2007-01-19 17:29
(Received via mailing list)
Ok, but that's what I did in the first place, but it doesn't allow
reading line by line... that is, it looks to me that you can

1) reading as chunks come in

OR

2) reading line by line

but you can't

"reading lines as they come in..."

(that is, 1) AND 2) as it would be the case with
"BufferedReader.readLine()" in Java).

In other words what I wanted is (pseudo-code):

response_handler = get_response_handler
while( line = response_handler.getLine())
{
  doSomethingWith(line)
}

without having to wait for the whole response to be read before starting
parsing...
Daniel DeLorme (Guest)
on 2007-01-22 09:18
(Received via mailing list)
Scara Maccai wrote:
>
> "reading lines as they come in..."

chunks are made out of lines; it's not so difficult to process each
line as the chunks come in:

buffer = ""
req = Net::HTTP.new('10.2.1.2')
req.post('/mycgi', mydata) do |chunk|
   buffer << chunk
   while buffer.sub!(/^(.*)\n/,"")
     do_something_with_line($1)
   end
end

Daniel
Scara Maccai (Guest)
on 2007-01-22 10:50
(Received via mailing list)
Ok, that's what I was looking for!
Thank you everybody
This topic is locked and can not be replied to.