Skip to content

How to Send an HTTP Request Through a Proxy with cURL

Use cURL to test an HTTP or SOCKS5 proxy from the command line: the right flags, authentication, environment variables, a verification routine and how to read errors.

9 min readUpdated Nov 13, 2026By XPASS Editorial Team

How to Send an HTTP Request Through a Proxy with cURL

When a proxy does not behave the way you expect, the fastest way to find out why is to take your application and framework out of the picture and talk to the proxy directly with cURL. It is installed on nearly every Linux and macOS machine, it ships with current Windows, and its output is detailed enough to show each step of the connection. This guide covers the flags that matter when sending HTTP requests through a proxy, how to authenticate, how to use environment variables and configuration files, how to read the verbose output, and what the common error messages mean.

As with every proxy topic, keep the use lawful. Send requests to your own services, to test endpoints, or to sites where you are permitted to make automated requests, follow their terms and local law, and keep your request rate modest. cURL makes it easy to send a request; it does not make every request appropriate.

The basic command

The core of the job is one option: -x (long form --proxy). The simplest form is:

curl -x http://proxy.example.net:8080 https://example.com/

For an HTTP target, cURL sends the request to the proxy, which forwards it. For an HTTPS target, cURL first sends a CONNECT example.com:443 request to the proxy, as explained in the MDN overview of proxy servers and tunneling. Once the proxy answers 200, cURL performs the TLS handshake with the real site through that tunnel, so the proxy cannot read the encrypted content. You do not need an extra flag for this; cURL chooses tunneling automatically for HTTPS. If you ever need to force a tunnel for plain HTTP, add -p (--proxytunnel).

Before running anything, gather the four details from your provider's dashboard or the proxy product page: host, port, protocol and authentication method. The examples here use the placeholders proxy.example.net and 8080.

Choosing the proxy type with the scheme

cURL decides what kind of proxy it is talking to from the scheme you write in front of the address.

Scheme Meaning Name resolution
http:// HTTP proxy (also the default when no scheme is given) Target host is sent to the proxy in the request
https:// The connection to the proxy itself is encrypted Same as HTTP, with TLS to the proxy
socks5:// SOCKS5 proxy, as defined in RFC 1928 cURL resolves the target name locally
socks5h:// SOCKS5 proxy with remote name resolution The proxy resolves the target name
socks4://, socks4a:// The older SOCKS4 protocol Local and remote respectively

The difference between socks5 and socks5h is one of the most useful details to remember. With socks5://, your own machine performs the DNS lookup for the target and hands the proxy an IP address, which means the lookup is visible on your local network and may return a different answer than the proxy's region would. With socks5h://, the name goes to the proxy, which resolves it. When you test regional behavior, socks5h:// is usually what you want.

Authentication

Most providers authenticate in one of two ways: an allowlist of source IP addresses, or a username and password. With an allowlist, no extra flags are needed. With credentials, use --proxy-user (short form -U):

curl -x http://proxy.example.net:8080 --proxy-user "$PROXY_USER:$PROXY_PASS" https://example.com/

By default cURL starts with Basic authentication. If the proxy replies 407 Proxy Authentication Required and advertises another scheme, you can let cURL negotiate with --proxy-anyauth, or force a specific one with options such as --proxy-basic or --proxy-digest. Basic authentication sent over a plain HTTP connection to the proxy is readable by anyone on that network path, so when your provider offers an encrypted proxy connection or a SOCKS5 option, prefer it for sensitive use.

Credentials need care in the shell:

  • Do not type real passwords directly into the command line. They end up in shell history and in the process list while the command runs.
  • Read them from environment variables, as in the example, or store them in a config file that only your user can read and load it with -K.
  • If a password contains special characters and you put it in a URL such as http://user:p%40ss@host:8080, percent-encode the characters. Using --proxy-user avoids that problem.
  • Never paste commands containing real credentials into tickets, chats or screenshots. Replace them with placeholders first.

Environment variables and config files

You can avoid repeating -x by setting environment variables. cURL honors lowercase http_proxy, https_proxy, all_proxy and no_proxy:

export https_proxy="http://proxy.example.net:8080"
export no_proxy="localhost,127.0.0.1,.internal.example.com"
curl https://example.com/

Two details trip people up. First, for the plain HTTP variable cURL deliberately ignores an uppercase HTTP_PROXY, because that name can be set by web servers from request headers in some CGI environments; use lowercase http_proxy for that one. Second, no_proxy is a comma-separated list of host names or domain suffixes that must bypass the proxy, and forgetting it is how local services end up sent to an external proxy.

For one request you can override or disable everything with --noproxy "*" or -x "", which is a handy way to produce the "without proxy" half of a comparison. For a repeatable setup, a .curlrc file in your home directory, or a file passed with -K, can hold lines such as proxy = "http://proxy.example.net:8080". Keep such files out of version control.

Verifying the route

A good routine has three parts. First, request a page that returns the caller's public IP address, once with the proxy and once without, and compare the answers. Use an echo endpoint you host or one your team already trusts:

curl -s https://ip-echo.example.com/
curl -s -x http://proxy.example.net:8080 https://ip-echo.example.com/

The second output should be the proxy's exit address, not yours. If your provider documents the exit region, check that it matches.

Second, use -v (verbose) to watch the connection:

curl -v -x http://proxy.example.net:8080 https://example.com/ -o /dev/null

In the output, lines beginning with * describe connection events, > lines are what cURL sent, and < lines are what it received. For an HTTPS target you should see a CONNECT example.com:443 HTTP/1.1 request, a HTTP/1.1 200 response from the proxy, and then the TLS handshake. If instead you see 407, authentication is the problem. If the connection never reaches the proxy, you will see a failure at the first step.

Third, capture measurements with -w, the write-out option, to log status code and timing without printing the page:

curl -s -o /dev/null -x http://proxy.example.net:8080 \
  -w "status=%{http_code} connect=%{time_connect}s total=%{time_total}s\n" \
  https://example.com/

Use --connect-timeout 10 and --max-time 30 so a dead proxy fails quickly instead of hanging your script.

A useful habit is to change one variable at a time. Run the same request first without a proxy, then with the proxy and verbose output, then with a different target URL. If only the proxied run fails, look at the proxy address, port, scheme and credentials. If every run fails against one target but works against others, the problem is probably the target or its rules, not the route. Write down the result of each run, including the status line and the time it took, so you can compare notes with a colleague or with your provider without guessing.

Keep a small text file of working commands for your team. Include the command that prints only the status code, the command that shows the exit address, and the command with verbose output, each with placeholders instead of real credentials. When a new colleague has a proxy problem, they can run the three commands in order and report the output, which is usually enough to see whether the issue is the address, the credentials or the application. Review the file whenever the proxy details change, and never paste real passwords into shared notes or tickets.

A plain example: debugging a failing application

Suppose a script that fetches your own status page works from your laptop but fails from a server with the error "proxy connection refused". A short debugging session might look like this:

  1. From the server, run curl -v -x http://proxy.example.net:8080 https://status.example.com/. If you see Connection refused on the first line, the server cannot reach the proxy port at all, which points to a firewall or a wrong port.
  2. If the connection works but you get 407, the credentials are missing or wrong, or the server's IP address is not on the allowlist.
  3. If you get 200 on the CONNECT line but then a certificate error, check whether something between you and the site is intercepting TLS, and check that the server's clock and CA bundle are correct.
  4. If cURL succeeds from the command line, the problem is in the application: it is probably not reading the same proxy settings. Compare the variables available to the application process with your shell, and then look at the client library guide, for example setting up a proxy in Node.js fetch.

Limits and what to watch for

  • 407 versus 403. 407 means the proxy wants credentials. 403 from the proxy usually means the destination or your account is not allowed there. A 403 from the destination site is a different matter and may mean the site does not want your request.
  • 502, 503, 504. These indicate that the proxy could not reach the destination, is overloaded, or timed out. Retry sparingly with a delay, not in a tight loop.
  • 429. If the target returns 429 Too Many Requests, slow down and honor Retry-After as defined in RFC 9110. A proxy is not a reason to ignore rate limits.
  • Certificate errors. Avoid -k (--insecure) as a habit. It disables certificate checks and hides real problems. Use it only briefly on a test host you control, to confirm a diagnosis.
  • Redirects. By default cURL does not follow redirects. Add -L if you need it, and remember that each hop also goes through the proxy.
  • Concurrency. Running many cURL commands in parallel may exceed the connection limits of your plan. Check the plan page for what is included.
  • Rotating exits. If the endpoint rotates addresses, two consecutive requests may show different IPs. That is expected; use a sticky or dedicated address if you need stability.

Where to go next

Once the command line works, you can carry the same proxy details into code. The article on using a proxy with Scrapy for permitted public data shows how to apply them in a crawler with polite settings, and setting up a proxy in Selenium for QA work does the same for browser tests. In every case, keep a cURL one-liner in your runbook as the first thing to try when something fails, because it separates network and credential problems from application problems in seconds.

Summary

To send a request through a proxy with cURL, use -x with a scheme that matches the proxy type, add --proxy-user when credentials are required, and prefer socks5h:// when you want the proxy to resolve names. Use lowercase environment variables and no_proxy for repeated setups, read secrets from the environment rather than typing them, and verify the result with an IP echo page, -v output and -w measurements. Treat error codes as information: 407 is authentication, 5xx is the proxy or the route, and 429 means slow down. Keep every request lawful and polite, and when you want to see which protocols and options a plan offers, look at the proxy page.

Frequently asked questions

Which cURL option sends a request through a proxy?

Use -x or --proxy followed by the proxy address, for example curl -x http://proxy.example.net:8080 https://example.com/. The scheme in the address chooses the proxy type, such as http:// for an HTTP proxy or socks5h:// for SOCKS5.

What is the difference between socks5 and socks5h in cURL?

With socks5:// cURL resolves the target host name on your own machine and sends the IP address to the proxy. With socks5h:// the host name is passed to the proxy, which resolves it. The h variant keeps DNS lookups on the proxy side.

How do I give cURL a proxy username and password safely?

Use --proxy-user with values read from environment variables or a config file with restricted permissions, rather than typing them into the command where they land in shell history. Avoid sharing command lines that contain real credentials.

How can I check that cURL really used the proxy?

Add -v to see the CONNECT line and the proxy address in the output, and request a page that echoes your public IP address. Compare the result with the same command run without the proxy.

About the author

Bob Kanokphon Thongdee, founder of XPASS VPN

Bob Kanokphon Thongdee

  • Founder of XPASS VPN
  • Co-founder of eSIMonline.asia
  • More than 7 years in online marketing
  • Founder of Cojourney Visa and Cojourney Travel

Need a proxy for this?

XPASS Proxy sells residential proxy traffic by the gigabyte, with the price shown before you pay.

See proxy plans

Keep reading

Proxy Guides

How to Use a Proxy with Scrapy for Permitted Public Data

Learn how to route Scrapy spiders through a proxy for public data you are allowed to collect, using request meta, a downloader middleware and polite crawl settings.

10 min read

Proxy Guides

How to Set Up a Proxy in Node.js fetch

The built-in fetch in Node.js ignores proxy environment variables by default. This guide shows how to route it through a proxy with an undici dispatcher and test the result.

9 min read

Proxy Guides

How to Set Up a Proxy in Selenium for QA Work

A practical guide to routing Selenium WebDriver sessions through a proxy for regional and environment testing, with Chrome and Firefox examples and a verification routine.

9 min read