raleng

raleng

HTTPS request using Req and DNS over HTTPS

Hey everyone,

I’m still pretty new to Elixir so I don’t have any experience with any HTTP library, but ran into an issue using Req. Since I was able to solve my problem using HTTPoisen, this post is more out of curiosity, whether there is a solution using Req and what I might have missed/misunderstood.

Problem

I want to fetch a JSON file from a HTTPS URL using a DNS over HTTPS service (Cloudflare’s, in this case). The JSON file might be hosted at the resolved IP address, or it might redirect yet another time.

I put up some sample projects for my own testing (they are still live):
https://vercel-redirect-test-json.vercel.app/some.json
redirects to
https://vercel-static-test-json.vercel.app/some.json
where some.json is hosted.

Using cURL and HTTPoison works!

In the terminal I use dig to look up the IP address and can resolve the JSON file using cURL with the --resolve option:

curl -L --resolve vercel-redirect-test-json.vercel.app:443:76.76.21.61 \
    https://vercel-redirect-test-json.vercel.app/some.json

I was able to get the request working using HTTPoison and hackney:

resolved_ip = "76.76.21.61" # this would be the result of the dns-query to Cloudflare
domain = "vercel-redirect-test-json.vercel.app"
resolve_option = {:resolve, [{domain, 443, ip}]

HTTPoison.get(
  "https://#{domain}/some.json",
  [],
  follow_redirect: true,
  hackney: [options: [resolve_option]]
)

How to use Req?

I did not find anything like a resolve option, and while I can resolve the “statically” hosted file by specifically providing the hostname to avoid any SSL errors,

resolved_ip = "76.76.21.61" # this would be the result of the dns-query to Cloudflare
domain = "vercel-static-test-json.vercel.app"
request =
  Req.new(
    method: :get,
    url: "https://#{ip}/some.json",
    connect_options: [hostname: domain]
  )

Req.get(request) # this works, the response has the JSON object!

I get repeated debug logs when trying to use the redirected version https://vercel-redirect-test-json.vercel.app/some.json

[debug] redirecting to https://vercel-static-test-json.vercel.app/some.json

and error out in the end with ** (RuntimeError) too many redirects (10).

What am I doing wrong?

Where Next?

Popular in Questions Top

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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vegabook
I'm brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42842 311
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30840 112
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126226 1237
New

We're in Beta

About us Mission Statement