{"id":1107,"date":"2026-10-13T19:17:27","date_gmt":"2026-10-13T16:17:27","guid":{"rendered":"https:\/\/mexela.com\/blog\/rust-reqwest-proxy\/"},"modified":"2026-11-22T13:51:26","modified_gmt":"2026-11-22T11:51:26","slug":"rust-reqwest-proxy","status":"publish","type":"post","link":"https:\/\/mexela.com\/blog\/rust-reqwest-proxy\/","title":{"rendered":"How to Use Proxies in Rust with reqwest"},"content":{"rendered":"<p class=\"mexela-answer\">To use a proxy in Rust, configure a <code>reqwest::Client<\/code> with <code>Proxy::all()<\/code>, add the proxy&#8217;s login details, and send your request through that client. A good first test is an IP lookup. It gives you something concrete to compare before you put the same settings into your application.<\/p>\n<p>This tutorial starts with a small command-line program. You&#8217;ll get one request working, check the result, and then adapt it for IP authentication, SOCKS5, or an existing async project.<\/p>\n<h2 id=\"runtime\">Set up a small Rust project<\/h2>\n<p>Open a terminal and create a project.<\/p>\n<pre><code class=\"language-bash\">cargo new rust-proxy-demo\ncd rust-proxy-demo<\/code><\/pre>\n<p>Add this dependency to <code>Cargo.toml<\/code>, underneath the existing <code>[dependencies]<\/code> heading.<\/p>\n<pre><code class=\"language-toml\">reqwest = { version = \"=0.13.5\", default-features = false, features = [\"blocking\", \"rustls\", \"socks\"] }<\/code><\/pre>\n<p>The examples use reqwest 0.13.5. The <code>blocking<\/code> feature lets this little program run with a regular <code>main()<\/code> function, <code>rustls<\/code> enables HTTPS, and <code>socks<\/code> adds SOCKS support. You can find the available features in the <a href=\"https:\/\/docs.rs\/reqwest\/0.13.5\/reqwest\/#optional-features\">reqwest documentation<\/a>.<\/p>\n<p>If your application already runs on Tokio, jump to the async section below after reading the proxy setup. For a standalone script, the blocking client keeps the first example easier to follow.<\/p>\n<h2 id=\"example\">Make your first request through the proxy<\/h2>\n<p>You&#8217;ll need the proxy host, port, username, and password from your provider. Set them as environment variables so you can change the connection without editing the Rust file. The values below are placeholders.<\/p>\n<p>On Linux or macOS, use the following commands.<\/p>\n<pre><code class=\"language-bash\">export PROXY_ENDPOINT='http:\/\/proxy.example:8080'\nexport PROXY_USER='your-proxy-user'\nexport PROXY_PASSWORD='your-proxy-password'\nexport CHECK_URL='https:\/\/api.ipify.org'<\/code><\/pre>\n<p>In Windows PowerShell, set the same four variables like this.<\/p>\n<pre><code class=\"language-powershell\">$env:PROXY_ENDPOINT = 'http:\/\/proxy.example:8080'\n$env:PROXY_USER = 'your-proxy-user'\n$env:PROXY_PASSWORD = 'your-proxy-password'\n$env:CHECK_URL = 'https:\/\/api.ipify.org'<\/code><\/pre>\n<p>Use the scheme and port your provider specifies. For example, <code>http:\/\/<\/code> at the start of the proxy address describes the connection to the proxy. Your destination can still be an HTTPS website.<\/p>\n<p>Replace <code>src\/main.rs<\/code> with this code.<\/p>\n<pre><code class=\"language-rust\">use reqwest::{blocking::Client, Proxy};\nuse std::{env, error::Error, time::Duration};\n\nfn main() -&gt; Result&lt;(), Box&lt;dyn Error&gt;&gt; {\n    let proxy = Proxy::all(env::var(&quot;PROXY_ENDPOINT&quot;)?)?.basic_auth(\n        &amp;env::var(&quot;PROXY_USER&quot;)?,\n        &amp;env::var(&quot;PROXY_PASSWORD&quot;)?,\n    );\n\n    let client = Client::builder()\n        .no_proxy()\n        .proxy(proxy)\n        .connect_timeout(Duration::from_secs(5))\n        .timeout(Duration::from_secs(20))\n        .build()?;\n\n    let response = client\n        .get(env::var(&quot;CHECK_URL&quot;)?)\n        .send()\n        .map_err(reqwest::Error::without_url)?;\n\n    println!(&quot;HTTP {}&quot;, response.status());\n\n    let body = response\n        .error_for_status()\n        .and_then(|response| response.text())\n        .map_err(reqwest::Error::without_url)?;\n\n    println!(&quot;{}&quot;, body.trim());\n    Ok(())\n}<\/code><\/pre>\n<p>Run it from the project directory.<\/p>\n<pre><code class=\"language-bash\">cargo run<\/code><\/pre>\n<p>When the request succeeds, you should see an HTTP status followed by the public IP reported for the connection. This is an example of the output, using a documentation-only address.<\/p>\n<pre><code class=\"language-text\">HTTP 200 OK\n203.0.113.42<\/code><\/pre>\n<p>The first build takes longer because Cargo downloads and compiles the dependencies. Later runs reuse that work. Keep the generated <code>Cargo.lock<\/code> with this application if you want subsequent builds to resolve the same dependency versions.<\/p>\n<p>The code prints the status before reading the body. It also turns HTTP 4xx and 5xx responses into errors, so a failed request doesn&#8217;t quietly look like a successful IP lookup. The IP service used here returns the caller&#8217;s public address. You can also set <code>CHECK_URL<\/code> to a test endpoint you control. See the <a href=\"https:\/\/www.ipify.org\/\">ipify API<\/a> and <a href=\"https:\/\/docs.rs\/reqwest\/0.13.5\/reqwest\/blocking\/struct.Response.html#method.error_for_status\">reqwest response handling<\/a>.<\/p>\n<h2 id=\"routing\">Check which IP your request is using<\/h2>\n<p>Compare the result with a direct request from the same machine. With curl installed, this command bypasses its proxy settings for the lookup.<\/p>\n<pre><code class=\"language-bash\">curl --noproxy '*' https:\/\/api.ipify.org<\/code><\/pre>\n<p>In PowerShell, use <code>curl.exe --noproxy '*' https:\/\/api.ipify.org<\/code> so the command runs curl itself.<\/p>\n<p>Compare both addresses with the exit IP your provider expects. An IP-check response shows the address that particular service saw. If you need to confirm access to your own API, make a second request to that API and check its logs too.<\/p>\n<p>In the Rust example, <code>.no_proxy()<\/code> comes before <code>.proxy(proxy)<\/code>. It clears inherited proxy settings, then the next call adds the explicit proxy you supplied. The order matters. Calling <code>.no_proxy()<\/code> after adding your proxy would clear it again. See <a href=\"https:\/\/docs.rs\/reqwest\/latest\/reqwest\/blocking\/struct.ClientBuilder.html#method.no_proxy\">ClientBuilder::no_proxy()<\/a>.<\/p>\n<p><code>Proxy::all()<\/code> covers HTTP and HTTPS destinations. If you use <code>Proxy::http()<\/code> instead, its rule covers HTTP destination URLs. That&#8217;s an easy mistake to miss when your test URL starts with <code>https:\/\/<\/code>. The rule names and examples are documented in the <a href=\"https:\/\/docs.rs\/reqwest\/0.13.5\/reqwest\/struct.Proxy.html\">Proxy API<\/a>.<\/p>\n<h2 id=\"credentials\">Using a proxy with IP authentication<\/h2>\n<p>Some proxies authorize your machine&#8217;s public IP instead of asking for a username and password. If that&#8217;s how your endpoint works, replace the first proxy declaration in <code>main()<\/code> with this line.<\/p>\n<pre><code class=\"language-rust\">let proxy = Proxy::all(env::var(\"PROXY_ENDPOINT\")?)?;<\/code><\/pre>\n<p>The rest of the program stays the same. Add the public IP of the machine running Rust to your provider&#8217;s allowlist. When you move the program from your laptop to a VPS, check the VPS address and authorize it as well.<\/p>\n<p>With username-and-password authentication, <code>Proxy::basic_auth()<\/code> supplies the proxy credentials. If the destination API also has a login, configure that on the request separately. For example, a destination bearer token belongs on <code>client.get(url).bearer_auth(token)<\/code>. Our <a href=\"\/blog\/proxy-authentication-username-password-vs-ip-auth\/\">proxy authentication guide<\/a> covers both access methods.<\/p>\n<h2 id=\"tls-connect\">Why an HTTP proxy works with an HTTPS URL<\/h2>\n<p>You may have an address such as <code>http:\/\/proxy.example:8080<\/code> and want to request <code>https:\/\/api.example.com<\/code>. That&#8217;s a normal setup. reqwest asks the HTTP proxy to open a CONNECT tunnel, then establishes TLS with the destination through that tunnel.<\/p>\n<p>Keep the proxy scheme your provider gave you. Changing it to <code>https:\/\/<\/code> asks for TLS on the connection to the proxy itself, which requires an endpoint that supports it. Basic proxy credentials sent over a plain HTTP connection are not encrypted on that hop.<\/p>\n<p>Certificate checks stay enabled in the example. If a certificate error appears, check the system clock, destination certificate, and any company TLS inspection first. A network that uses a private certificate authority may need that CA added to the client.<\/p>\n<pre><code class=\"language-rust\">let pem = std::fs::read(\"company-ca.pem\")?;\nlet certificate = reqwest::Certificate::from_pem(&amp;pem)?;\n\n\/\/ Add this while building the client, before .build().\nlet builder = Client::builder()\n    .no_proxy()\n    .proxy(proxy)\n    .tls_certs_merge([certificate]);<\/code><\/pre>\n<p>Use a CA provided by your network administrator. <code>tls_certs_merge()<\/code> adds it alongside the existing trust store. The <a href=\"https:\/\/docs.rs\/reqwest\/latest\/reqwest\/blocking\/struct.ClientBuilder.html#method.tls_certs_merge\">client certificate documentation<\/a> explains this setting. For the protocol differences, see our <a href=\"\/blog\/http-https-socks5-proxies\/\">HTTP, HTTPS, and SOCKS5 guide<\/a>.<\/p>\n<h2 id=\"socks\">Switch to SOCKS5<\/h2>\n<p>The dependency configuration above already includes the <code>socks<\/code> feature. To use a SOCKS5 endpoint that resolves destination hostnames at the proxy, change the endpoint variable.<\/p>\n<pre><code class=\"language-bash\">export PROXY_ENDPOINT='socks5h:\/\/proxy.example:1080'\ncargo run<\/code><\/pre>\n<p>In PowerShell, set <code>$env:PROXY_ENDPOINT = 'socks5h:\/\/proxy.example:1080'<\/code> and run <code>cargo run<\/code> again. Use your provider&#8217;s actual SOCKS port and credentials.<\/p>\n<p>The <code>h<\/code> in <code>socks5h<\/code> requests proxy-side resolution of the destination hostname. With <code>socks5<\/code>, the client resolves that destination locally. The proxy host itself still needs to be reachable from your machine. The main example continues to use the same client builder and login settings. This distinction is implemented in <a href=\"https:\/\/docs.rs\/reqwest\/0.13.5\/src\/reqwest\/connect.rs.html\">reqwest&#8217;s SOCKS connector<\/a>.<\/p>\n<h2 id=\"async\">If your project already uses Tokio<\/h2>\n<p>An async application uses <code>reqwest::Client<\/code> and awaits the response. The proxy configuration is the same. Add Tokio to the existing dependencies.<\/p>\n<pre><code class=\"language-toml\">tokio = { version = \"1\", features = [\"macros\", \"rt-multi-thread\"] }<\/code><\/pre>\n<p>Replace <code>src\/main.rs<\/code> with the following async version and run <code>cargo run<\/code> again. It reads the same environment variables as the first example. In the download, this version is a separate binary you can run with <code>cargo run --locked --bin async-example<\/code>.<\/p>\n<pre><code class=\"language-rust\">use reqwest::{Client, Proxy};\nuse std::{env, error::Error, time::Duration};\n\n#[tokio::main]\nasync fn main() -&gt; Result&lt;(), Box&lt;dyn Error&gt;&gt; {\n    let proxy = Proxy::all(env::var(&quot;PROXY_ENDPOINT&quot;)?)?.basic_auth(\n        &amp;env::var(&quot;PROXY_USER&quot;)?,\n        &amp;env::var(&quot;PROXY_PASSWORD&quot;)?,\n    );\n\n    let client = Client::builder()\n        .no_proxy()\n        .proxy(proxy)\n        .connect_timeout(Duration::from_secs(5))\n        .timeout(Duration::from_secs(20))\n        .build()?;\n\n    let response = client\n        .get(env::var(&quot;CHECK_URL&quot;)?)\n        .send()\n        .await\n        .map_err(reqwest::Error::without_url)?;\n\n    println!(&quot;HTTP {}&quot;, response.status());\n\n    let response = response\n        .error_for_status()\n        .map_err(reqwest::Error::without_url)?;\n    let body = response\n        .text()\n        .await\n        .map_err(reqwest::Error::without_url)?;\n\n    println!(&quot;{}&quot;, body.trim());\n    Ok(())\n}<\/code><\/pre>\n<p>Choose the client that fits the application around it. reqwest&#8217;s blocking client runs on a regular thread, while the async client works with Tokio. See the <a href=\"https:\/\/docs.rs\/reqwest\/latest\/reqwest\/blocking\/index.html\">blocking API guidance<\/a>.<\/p>\n<h2 id=\"diagnostics\">When the request fails<\/h2>\n<p>Start with the message you actually received. A rejected login and an unreachable port need different fixes.<\/p>\n<table>\n<thead>\n<tr>\n<th>What you see<\/th>\n<th>What to check first<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>HTTP 407<\/td>\n<td>Proxy credentials, account status, and the authentication method required by that endpoint.<\/td>\n<\/tr>\n<tr>\n<td>A connection or tunnel error<\/td>\n<td>Proxy host, port, scheme, and whether the machine can reach that address. A rejected CONNECT login can also surface here before a destination response exists.<\/td>\n<\/tr>\n<tr>\n<td>A timeout<\/td>\n<td>Whether the proxy connects, whether the destination responds, and whether your configured limits fit the request.<\/td>\n<\/tr>\n<tr>\n<td>A certificate error<\/td>\n<td>Destination hostname, certificate chain, system clock, and any private CA used by your network.<\/td>\n<\/tr>\n<tr>\n<td>HTTP 403 or 429<\/td>\n<td>Destination access rules and request limits. Try the same endpoint with a single request before adding concurrency.<\/td>\n<\/tr>\n<tr>\n<td>An environment-variable error before the request<\/td>\n<td>That all four variables were set in the terminal where you ran <code>cargo run<\/code>.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The example sets a five-second connection timeout and a twenty-second request timeout. Adjust those values for the API you&#8217;re using. A slow download and a quick IP lookup may need different limits.<\/p>\n<p>The two <code>without_url()<\/code> calls keep the request URL out of reqwest&#8217;s printed errors. That&#8217;s handy when an API uses query parameters for sensitive values. The error itself still propagates to the terminal. Applications that need structured handling can also inspect <code>is_timeout()<\/code> and <code>is_connect()<\/code>. See <a href=\"https:\/\/docs.rs\/reqwest\/0.13.5\/reqwest\/struct.Error.html\">reqwest&#8217;s Error reference<\/a>.<\/p>\n<h2 id=\"faq\">A few things to keep in mind as the script grows<\/h2>\n<h3>Should I build a client for every request?<\/h3>\n<p>Create the configured client once and reuse it for requests using that proxy. reqwest keeps a connection pool, so building a fresh client inside every loop adds avoidable setup work. The <a href=\"https:\/\/docs.rs\/reqwest\/0.13.5\/reqwest\/struct.Client.html\">Client documentation<\/a> describes connection reuse.<\/p>\n<h3>Can I use environment proxy settings instead?<\/h3>\n<p>Yes. A default native reqwest client can read <code>HTTP_PROXY<\/code>, <code>HTTPS_PROXY<\/code>, and <code>ALL_PROXY<\/code>. The tutorial configures the proxy explicitly to make each run easier to inspect. If your deployment manages proxy settings centrally, use the environment-based setup described in <a href=\"\/blog\/http-proxy-https-proxy-no-proxy\/\">our environment variable guide<\/a>.<\/p>\n<h3>Will a new client give me a new IP?<\/h3>\n<p>That depends on the proxy service. A fixed endpoint may keep the same exit address. A rotating service may use a session identifier, a rotation interval, or connection-based rules. Check those details with your provider before adding rotation logic to Rust.<\/p>\n<h3>Where should I go after the IP check works?<\/h3>\n<p>Use the same client for one request to your actual API. Confirm the expected response, then add the rest of your application&#8217;s logic. If you need help checking the route, our <a href=\"\/blog\/test-if-your-proxy-is-working\/\">proxy verification guide<\/a> walks through the next checks.<\/p>\n<h2 id=\"results\">Examples and testing<\/h2>\n<p><strong>Updated November 22, 2026.<\/strong> The blocking and async programs were compiled with Rust 1.99.0 and reqwest 0.13.5 on Ubuntu 24.04 in WSL2. Both passed local checks for HTTP forwarding, proxy authentication failures, HTTPS CONNECT, certificate validation, and SOCKS5 with the destination hostname passed to the proxy.<\/p>\n<p>You can download the <a href=\"\/blog\/wp-content\/uploads\/2026\/11\/mexela-rust-proxy-tutorial-20261122.zip\">source files for this tutorial<\/a>, including the blocking and async examples and their dependency lockfile.<\/p>\n<h2 id=\"versioned-source-repository\">More Rust examples<\/h2>\n<p>Our earlier <a href=\"\/blog\/wp-content\/uploads\/2026\/10\/mexela-rust-proxy-examples-20261013-v1.zip\">diagnostic example bundle<\/a> includes additional route, authentication, and TLS checks. You can also browse the <a href=\"\/blog\/proxy-code-examples-repository\/#rust\">Rust examples in the source repository<\/a> or return to the <a href=\"\/blog\/proxy-setup-developer-guides\/\">developer guides<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Get a Rust request working through a proxy, check its public IP, and adapt the example for authentication, HTTPS, SOCKS5, and Tokio.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[189],"tags":[],"_links":{"self":[{"href":"https:\/\/mexela.com\/blog\/wp-json\/wp\/v2\/posts\/1107"}],"collection":[{"href":"https:\/\/mexela.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mexela.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mexela.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mexela.com\/blog\/wp-json\/wp\/v2\/comments?post=1107"}],"version-history":[{"count":1,"href":"https:\/\/mexela.com\/blog\/wp-json\/wp\/v2\/posts\/1107\/revisions"}],"predecessor-version":[{"id":1122,"href":"https:\/\/mexela.com\/blog\/wp-json\/wp\/v2\/posts\/1107\/revisions\/1122"}],"wp:attachment":[{"href":"https:\/\/mexela.com\/blog\/wp-json\/wp\/v2\/media?parent=1107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mexela.com\/blog\/wp-json\/wp\/v2\/categories?post=1107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mexela.com\/blog\/wp-json\/wp\/v2\/tags?post=1107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}