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
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
3
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.
3
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
1
Popular in Questions
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
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
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
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
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
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
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
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
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
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Other popular topics
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
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
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
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
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
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
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
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
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
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








