v1.8.91-d84675c
← Back to Hex Proxies

Rust Reqwest Proxy Setup

Last updated: April 2026

By Hex Proxies Engineering Team

Integrate Hex Proxies into Rust applications using the reqwest crate. Covers HTTP and SOCKS5 proxy configuration, authentication, and async request patterns.

advanced20 minuteslanguage-integration

Prerequisites

  • Rust 1.70 or later
  • reqwest crate with proxy feature enabled
  • Hex Proxies account with credentials

Steps

1

Add reqwest to Cargo.toml

Include reqwest with the socks feature enabled for full protocol support.

2

Create a proxy instance

Use Proxy::all() with the gate.hexproxies.com URL including credentials.

3

Build the client

Chain .proxy() and .timeout() on reqwest::Client::builder().

4

Test connectivity

Send a GET request to httpbin.org/ip and verify the response shows a proxied IP.

5

Add retry logic

Implement exponential backoff for 429 and 503 responses.

6

Scale with concurrency

Clone the client across tokio::spawn tasks for parallel requests.

Rust Reqwest Proxy Integration

The reqwest crate is the de facto HTTP client for Rust. It supports proxy configuration through the `Proxy` builder, including HTTP, HTTPS, and SOCKS5 protocols.

Cargo.toml Dependencies

[dependencies]
reqwest = { version = "0.12", features = ["json", "socks"] }
tokio = { version = "1", features = ["full"] }

Basic HTTP Proxy

#[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { let proxy = Proxy::all("http://YOUR_USERNAME:YOUR_PASSWORD@gate.hexproxies.com:8080")?;

let client = reqwest::Client::builder() .proxy(proxy) .timeout(std::time::Duration::from_secs(30)) .build()?;

let resp = client.get("https://httpbin.org/ip").send().await?; println!("Status: {}", resp.status()); println!("Body: {}", resp.text().await?);

Ok(()) } ```

SOCKS5 Proxy

Enable the `socks` feature in your Cargo.toml, then use the socks5 scheme:

let client = reqwest::Client::builder() .proxy(proxy) .build()?; ```

Sticky Sessions

let session_user = "YOUR_USERNAME-session-rust001";
let proxy_url = format!(
    "http://{}:YOUR_PASSWORD@gate.hexproxies.com:8080",
    session_user
);
let proxy = Proxy::all(&proxy_url)?;

Concurrent Proxied Requests

let urls = vec![ "https://httpbin.org/ip", "https://httpbin.org/headers", "https://httpbin.org/get", ];

let tasks: Vec<_> = urls.into_iter().map(|url| { let client = client.clone(); tokio::spawn(async move { match client.get(url).send().await { Ok(resp) => println!("{}: {}", url, resp.status()), Err(e) => eprintln!("{}: {}", url, e), } }) }).collect();

join_all(tasks).await; ```

Retry with Backoff

async fn fetch_with_retry(
    client: &reqwest::Client,
    url: &str,
    max_retries: u32,
) -> Result<String, reqwest::Error> {
    let mut last_err = None;
    for attempt in 0..max_retries {
        match client.get(url).send().await {
            Ok(resp) if resp.status().is_success() => {
                return resp.text().await;
            }
            Ok(resp) if resp.status().as_u16() == 429 || resp.status().as_u16() == 503 => {
                tokio::time::sleep(std::time::Duration::from_secs(2u64.pow(attempt))).await;
            }
            Ok(resp) => {
                eprintln!("Non-retryable status: {}", resp.status());
                return resp.text().await;
            }
            Err(e) => {
                last_err = Some(e);
                tokio::time::sleep(std::time::Duration::from_secs(2u64.pow(attempt))).await;
            }
        }
    }
    Err(last_err.unwrap())
}

Environment-Based Configuration

let proxy_url = std::env::var("HEX_PROXY_URL")
    .expect("HEX_PROXY_URL must be set");
let proxy = Proxy::all(&proxy_url)?;

Reqwest also respects the standard `HTTP_PROXY` and `HTTPS_PROXY` environment variables if no explicit proxy is set on the client builder.

Tips

  • *Clone the reqwest Client freely -- it uses an Arc internally and shares the connection pool.
  • *Enable the socks feature in Cargo.toml before using SOCKS5 proxy URLs.
  • *Reqwest respects HTTP_PROXY and HTTPS_PROXY environment variables by default.
  • *Use the timeout builder method to prevent tasks from blocking indefinitely behind a slow proxy.

Ready to Get Started?

Put this guide into practice with Hex Proxies.

Cookie Preferences

We use cookies to ensure the best experience. You can customize your preferences below. Learn more