The proxy plugin allows one to send HTTP requests through a proxy.

It supports HTTP, HTTPS/CONNECT tunnel, Socks4, Socks4a and Socks5 out-of-the-box (and SSH proxies if you load the :proxy/ssh plugin).

All examples will assume the fictitious address/port pair. Replace the examples with your own.


After enabling the plugin, you can call a #proxy method, which can receive the following options:

  • :uri: the proxy URI or a list of proxy URIs (see examples below)
  • :username: proxy user (optional)
  • :password: proxy password (optional)
  • :scheme: proxy auth scheme (optional)
  • :no_proxy: list of domains/patterns that bypass the proxy (optional)
# Use an HTTP cache proxy to route clear-text HTTP requests:
HTTPX.plugin(:proxy).with_proxy(uri: "").get("")
# try another proxy if the first one fails
HTTPX.plugin(:proxy).with_proxy(uri: %w[]).get("")
# Use an HTTPS `CONNECT` proxy to tunnel secure HTTP(S) requests.
HTTPX.plugin(:proxy).with_proxy(uri: "").get("")
# Use a SOCKS proxy
HTTPX.plugin(:proxy).with_proxy(uri: "socks4://").get("")
HTTPX.plugin(:proxy).with_proxy(uri: "socks4a://").get("")
HTTPX.plugin(:proxy).with_proxy(uri: "socks5://").get("")

# no proxy
HTTPX.plugin(:proxy).with_proxy(uri: "", no_proxy: ["", /.*\.google\.com/]).get("")


httpx supports proxy authentication for all the supported HTTP authentication schemes:

proxy_http = HTTPX.plugin(:proxy)
# will do basic auth by default
proxy_http.with_proxy(uri: "", username: "user", password: "password").get("")
# can also explicitly call basic auth
proxy_http.with_proxy_basic_auth(uri: "", username: "user", password: "password").get("")
# proxy digest auth (since v0.20)
proxy_http.with_proxy_digest_auth(uri: "", username: "user", password: "password").get("")
# proxy ntlm auth (since v0.20)
proxy_http.with_proxy_ntlm_auth(uri: "", username: "user", password: "password").get("")


This plugin creates an SSH connection (using the net-ssh-gateway gem) and tunnels all requests from the session through it.

session = HTTPX.plugin(:"proxy/ssh")
               .with_proxy(uri: "ssh://",
                           username: "root",
                           password: "root")
response = session.get("")

ENV http_proxy/https_proxy/no_proxy

If the proxy environment variables are defined, HTTPX will load with the :proxy plugin turned on by default. It will therefore route or not route the requests through a proxy based on those environment variables.


What about socks5h?

“socks5h” is an old nomenclature for “socks5 and DNS resolution done in proxy”. However in practice, most implementations in the wild of socks5 default to sendig the domain to be resolved from the proxy, so they’re socks5h-compliant by default, and the :proxy plugin implementation of httpx is no exception. Next: Auth