views:

41

answers:

2

there is a space after the word Part in the file name I want to download. It looks like http.get doesn't pass the url_path correctly because you can download the file from the browser without any troubles.

any suggestion how I can download a file if there is a space in the file name?

require 'net/http'

url = "http://www.onalllevels.com/2009-12-02TheYangShow_Squidoo_Part 1.flv"

puts url_base = url.split('/')[2]
puts url_path = '/'+url.split('/')[3..-1].join('/')

Net::HTTP.start(url_base) do |http|
  resp = http.get(url_path)
  open("test.flv", "wb") do |file|
    file.write(resp.body)
  end
end
puts "Done."
+2  A: 

You need to properly encode your URL. Something like:

require 'uri'
val = URI.escape("my parameter value")
Beaner
@Beaner: could you add this code to your answer? `require 'uri';puts URI.escape('url to encode')`
Radek
+1  A: 

You could try replacing the space with a + or a %20.

Asaph
`+` didn't work for me but `%20` works nicely! Thank you.
Radek