Hello, actually I’m writing a package manager for Linux with Crystal.
I have just one problem, I don’t understand how to download a file with crystal.
For example if I want to download this archive:https://download.savannah.gnu.org/releases/acl/acl-2.3.1.tar.xz
How I can do ?
I’m assuming you just want to save the file to the file system? If so it would be something like:
HTTP::Client.get "https://download.savannah.gnu.org/releases/acl/acl-2.3.1.tar.xz" do |response|
File.open("out.tar.xz", "w") do |file|
IO.copy response.body_io, file
HTTP::Client doesn’t support redirects at the moment, see HTTP::Client: follow redirects · Issue #2721 · crystal-lang/crystal · GitHub, so you will probably have to add in some extra logic to handle that case. I.e. check for
response.status.redirection? and if
true, look for the
location header and make another request before copying the contents to the file.