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)

1 Like

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.

5 Likes

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!

7 Likes

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!()
2 Likes