Morzaram

Morzaram

Making a Req request with proxy

Hey there,

I’m trying to make a Req request with the following config, but I’m struggling a bit.

The curl I’m trying to make into a Req
curl --proxy my.proxy.com:22225 --proxy-user user:pass -k "myurl.com"

Req.new(
      url: url,
      connect_options: [
        proxy: {
          "http",
          "user:pass@my.proxy.com",
          22225
        }
      ]
    )
    |> Req.get()

When I run this I get:
no case clause matching: {:ok, {"http", "user:pass@my.proxy.com", 22225}}

I’m trying to understand what options to pass through with req, but it doesn’t have any examples for me to reference.

Any help would be appreciated (edited)

Most Liked

Morzaram

Morzaram

Hey thanks so much for responding!

The following code indeed worked.

Req.new(
  url: url,
  connect_options: [
    proxy_headers: [
      {"proxy-authorization", "Basic " <> Base.encode64("user:pass")}
    ],
    proxy: { :http, "my.proxy.com", 22225, [] },
    transport_opts: [ verify: :verify_none ]
  ]
)
|> Req.get()

I’ll make a PR with updated docs. And if I can understand what’s going on with setting http_proxy and https_proxy, I will make a PR for that too when I have a moment. This is a great library! thanks for your efforts on this!

wojtekmach

wojtekmach

Hex Core Team

I haven’t tested but this should work:

Req.get!(
  url,
  connect_options: [proxy: {:http, "localhost", 22225, []}],
  proxy_headers: [{"proxy-authorization", "basic " <> Base.encode64("user:pass")}]
)

There is hardly any documentation for this in Req (Req — req v0.5.6) and very scarce in Mint (Mint.HTTP — Mint v1.6.2) so if you get this to work, a PR with docs improvements would be very appreciated. We have an open issue, Support http_proxy and https_proxy environment variables · Issue #394 · wojtekmach/req · GitHub, to support http_proxy and https_proxy env vars so that would probably be even better way to solve this.

chgeuer

chgeuer

If your proxy has some sort of untrusted X509 cert but you want to use it anyway, you can also use :cacertfile instead of verify: :verify_none:

Req.new(
  url: "https://www.google.com/",
  connect_options: [
    proxy_headers: [
      {"proxy-authorization", "Basic " <> Base.encode64("user:pass")}
    ],
    proxy: { :http, "192.168.1.65", 8888, [] },
    transport_opts: [ 
      # verify: :verify_none,
      cacertfile: "/mnt/c/chgeuer/FiddlerRoot.pem"
    ]
  ]
)
|> Req.get!()
kevinschweikert

kevinschweikert

🥌 CurlReq — CurlReq v0.100.0 can now do this for you!

import CurlReq
~CURL(curl --proxy my.proxy.com:22225 --proxy-user user:pass -k “myurl.com”)

Result:

%Req.Request{
  method: :get,
  url: URI.parse("http://“myurl.com”"),
  headers: %{},
  body: nil,
  options: %{
    connect_options: [
      proxy: {:http, "my.proxy.com", 22225, []},
      proxy_headers: [{"proxy-authorization", "Basic dXNlcjpwYXNz"}],
      transport_opts: [verify: :verify_none]
    ]
  },
  halted: false,
  adapter: &Req.Steps.run_finch/1,
  request_steps: [],
  response_steps: [],
  error_steps: [],
  private: %{}
}

Where Next?

Popular in Questions Top

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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
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
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
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

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
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
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New

We're in Beta

About us Mission Statement