Skip to content

Instantly share code, notes, and snippets.

@wbingli
Created May 1, 2014 22:32
Show Gist options
  • Save wbingli/6f8ed65dded41fb9ccb2 to your computer and use it in GitHub Desktop.
Save wbingli/6f8ed65dded41fb9ccb2 to your computer and use it in GitHub Desktop.
Download big file with ruby and basic authentication
require 'net/http'
require 'uri'
def downloadFile(filePath, url,username, password)
uri = URI(URI.encode(url))
Net::HTTP.start(uri.host,uri.port) do |http|
request = Net::HTTP::Get.new uri.path
request.basic_auth username,password
http.request request do |response|
open filePath, 'w' do |io|
response.read_body do |chunk|
puts "Writing #{chunk.length} bits ..."
io.write chunk
end
end
end
end
end
urlPath = "http://<link_to_big_file>"
downloadFile '/tmp/test.zip',urlPath,"username","password"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment