4

In Ruby:

require 'open-uri'
download = open('http://example.com/download.pdf')
IO.copy_stream(download, '~/my_file.pdf')

How to do the same in Crystal?

Sun
  • 2,595
  • 1
  • 26
  • 43
SeventhSon
  • 71
  • 5

2 Answers2

10

We can do the following:

require "http/client"

HTTP::Client.get("http://example.org") do |response|
  File.write("example.com.html", response.body_io)
end

This writes just the response without any HTTP headers to the file. File.write is also smart enough to not download the entire file into memory first, but to write to the file as it reads chunks from the given IO.

Jonne Haß
  • 4,792
  • 18
  • 30
1

I found something that works:

require "http/request"
require "file"
res = HTTP::Client.get "https://ya.ru"
fl=File.open("ya.html","wb")
res.to_io(fl)
fl.close
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
SeventhSon
  • 71
  • 5