# Streaming media with net/http

**URL:** <https://www.ruby-forum.com/t/streaming-media-with-net-http/149242>\
**Category:** Ruby\
**Created:** [September 22, 2008, 8:33pm UTC](https://www.ruby-forum.com/t/streaming-media-with-net-http/149242 "2008-09-22T20:33:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wfspoet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e480ec/32.png) [@wfspoet](https://www.ruby-forum.com/u/wfspoet)\
**Post date:** [September 22, 2008, 8:33pm UTC](https://www.ruby-forum.com/t/streaming-media-with-net-http/149242/1 "2008-09-22T20:33:01Z")

</div>

So I was wondering how one would capture a continuous stream of data  
(for instance a net radio station or a webcam) with the net/http  
library? When I use the Net::HTTP#request\_get method, it simply hangs  
and never gives me any data. I opened up a network monitoring program  
and indeed the data is coming in, it’s just the only way I can get the  
data to stop streaming is to throw an exception, which makes the data  
disappear. Does anyone know of a way to do this?

---

<div class="post-metadata">

**Author:** ![wfspoet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e480ec/32.png) [@wfspoet](https://www.ruby-forum.com/u/wfspoet)\
**Post date:** [September 22, 2008, 9:10pm UTC](https://www.ruby-forum.com/t/streaming-media-with-net-http/149242/2 "2008-09-22T21:10:23Z")

</div>

On Tue, Sep 23, 2008 at 03:24:49AM +0900, William Spitzer wrote:

> So I was wondering how one would capture a continuous stream of data  
> (for instance a net radio station or a webcam) with the net/http  
> library?

Here’s an example of streaming the Ubuntu ISO with Net::HTTP:

require ‘net/http’  
uri =  
URI(“[http://mirrors.us.kernel.org/ubuntu-releases/hardy/ubuntu-8.04.1-desktop-i386.iso](http://mirrors.us.kernel.org/ubuntu-releases/hardy/ubuntu-8.04.1-desktop-i386.iso)”)  
Net::HTTP.get\_response(uri) do |res|  
size, total = 0, res.header[‘Content-Length’].to\_i  
res.read\_body do |chunk|  
size += chunk.size  
puts “%d%% done (%d of %d)” % [(size \* 100) / total, size, total]  
end  
end

So, before the `read_body` block, you can examine the headers. And  
inside the `read_body` block, you can handle chunks as they come in.  
In your case, you’d pass them to file.write.

\_why

---

<div class="post-metadata">

**Author:** ![wfspoet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e480ec/32.png) [@wfspoet](https://www.ruby-forum.com/u/wfspoet)\
**Post date:** [September 22, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/streaming-media-with-net-http/149242/3 "2008-09-22T23:55:23Z")

</div>

\_why wrote:

> ```
> size, total = 0, res.header['Content-Length'].to_i
> 
> ```
> 
> \_why

And don’t forgot to put a “stop” condition to the loop in case of a  
“everlasting download”. The problem is : I don’t know how he can cleanly  
break the chunk iterations (will the socket be cleanly disconnected with  
an exception etc.?).

By the way, this kind of “streaming” is named “progressive download” and  
both have their drawbacks.

regards

–FrihD(Lucas)
