shahryarjb

shahryarjb

How to download a file with Finch which is redirected

Hi, I want to download a file from GitHub, but Finch returns 302 status and can’t download.

for example:

 %Finch.Response{
   status: 302,
   body: "",
   headers: [
     {"server", "GitHub.com"},
     {"date", "Mon, 19 Sep 2022 11:41:40 GMT"},
     {"content-type", "text/html;charset=utf-8"},
     {"content-length", "0"},
     {"cache-control", "public, must-revalidate, max-age=0"},
     {"expires", "Mon, 19 Sep 2022 11:41:40 GMT"},
     {"location",
      "https://codeload.github.com/mishka-group/mishka-cms/legacy.tar.gz/refs/tags/0.0.2"},

In location section you can see the real link after redirection, if I use this it works, but I need to know how to force Finch download automatically links maybe are redirected.


Block code:

Finch.build(:get, "https://api.github.com/repos/mishka-group/mishka-cms/tarball/0.0.2")
|> Finch.request(@request_name)

Thank you in advance

Most Liked

hst337

hst337

There’s no built-in feature inside Finch which follow redirects. You can write your own wrapper for this. For example

def do_get(url, name, depth \\ 123)
def do_get(:undefined, _, _), do: {:error, :bad_redirect}
def do_get(url, name, 0), do: {:error, :maximum_depth_exceeded}
def do_get(url, name, depth) do
  :get
  |> Finch.build(url)
  |> Finch.request(name)
  |> case do
    {:ok, %Finch.Response{status: 302, headers: headers}} ->
      "location"
      |> :proplists.get_value(headers)
      |> do_get(name, depth - 1)

    other ->
      other
  end
end
dimitarvp

dimitarvp

Apologies that I am not answering your question directly but I would strongly recommend you use Tesla with Finch below. Tesla has a number of these super useful wrappers, redirect following being one of them.

hst337

hst337

As I’ve said, there’s no such option. I’ve read the sources and it doesn’t support redirects out of the box

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement