Reading an HTTPResponse line by line

S

Scara Maccai

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...
 
D

Dale Martenson

Scara said:
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
 
A

ara.t.howard

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...

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
 
S

Scara Maccai

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)
 
A

ara.t.howard

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
 
S

Scara Maccai

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...
 
D

Daniel DeLorme

Scara said:
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..."

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top