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

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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

Other popular topics Top

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
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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

We're in Beta

About us Mission Statement