Originally published on proxymint.
reqwest proxy setup for Rust on reqwest 0.13: Proxy::all, credentials, the socks feature, socks5h, env variables and NO_PROXY, pooling, rotation and errors.
TL;DR
- One call does the setup. Pass Proxy::all("http://USERNAME:PASSWORD@HOST:PORT") to ClientBuilder::proxy. HTTPS URLs then go through a CONNECT tunnel with no other option.
- Proxy::http only covers http:// URLs. The constructor names the target scheme, so a Proxy::http rule sends every https:// request direct. Use Proxy::all.
- SOCKS5 needs the socks feature. Without it a socks5h:// URL compiles and then fails on every request with unsupported scheme socks5h.
- An explicit proxy turns off the environment. After .proxy(), HTTPS_PROXY and NO_PROXY are ignored; add NoProxy::from_env() to keep the bypass list.
Key takeaways
- Percent-encode the proxy password. reqwest decodes %XX sequences in the proxy URL and in basic_auth(), so a password with a percent sign reaches the proxy changed unless it is encoded first.
- Use socks5h:// for SOCKS5. With socks5:// your own machine resolves the website's hostname; with socks5h:// the proxy does the lookup.
- Turn the pool off to rotate. One reqwest Client reuses its tunnel and its exit. pool_max_idle_per_host(0) opens a new connection per request; a client per proxy rotates a list without losing pooling.
- Read the error chain, not the top line. A rejected login on an https:// URL is an error ending in tunnel error: proxy authorization required, with is_connect() true and no status; on an http:// URL it is a 407 response.
Read the full guide on proxymint, with the commands, tables and sources.
