What does the code to download a file/image from a URL look like in Elixir?
Google searches seem to bring back how to download Elixir itself.
Have a look at httpoison, an HTTP client for Elixir. You can just issue a GET
request to the url pointing to the image (or file, it's irrelevant):
%HTTPoison.Response{body: body} = HTTPoison.get!("http://example.com/img.png")
HTTPoison.get!/1
returns an HTTPoison.Response
struct; I matched on that struct in order to get the body of the response. Now the body
variables contains the image data (which is just a binary); you can write it to a file:
File.write!("/tmp/image.png", body)
and there you go :).
This is obviously possible even without using httpoison, but you would have to deal with raw TCP connections (look at the gen_tcp
Erlang module), parsing of the HTTP response and a bunch of stuff you usually don't want to do manually.
Whoops, forgot to mention the httpc
Erlang module (included in the stdlib!), which makes this very simple without the need for a dependency like HTTPoison:
Application.ensure_all_started :inets {:ok, resp} = :httpc.request(:get, {'http://example.com/my_image.jpg', []}, [], [body_format: :binary]) {{_, 200, 'OK'}, _headers, body} = resp File.write!("/tmp/my_image.jpg", body)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With