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

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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
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
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I fore...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39247 209
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31107 143
New
hariharasudhan94
Lets say i have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => "XX...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement