Crystal lang如何从http获取二进制文件

Sev*_*Son 2 binary http crystal-lang

在Ruby中:

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

如何在水晶中做同样的事情?

Jon*_*Haß 7

我们可以做到以下几点:

require "http/client"

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

这只会将没有任何HTTP标头的响应写入文件.File.write也足够聪明,不能先将整个文件下载到内存中,而是在读取给定的块时写入文件IO.