Skip to main content

WP 301 Redirects

Use curl with a proxy by adding -x or --proxy before the proxy address, then place the target URL at the end. A typical command is curl -x http://proxy.example.com:8080 https://example.com. The official curl documentation lists proxy support for HTTP, HTTPS, SOCKS4, SOCKS5, and related options [source: curl]. This makes curl useful for testing APIs, checking access rules, scraping internal endpoints, and confirming whether traffic is actually leaving through the expected gateway.

TLDR: Use curl -x protocol://host:port URL to send a request through a proxy. For example, curl -x http://127.0.0.1:8080 https://api.example.com/status sends the HTTPS request through a local proxy on port 8080. A support engineer testing 50 regional API checks can route requests through separate proxies and spot blocked regions in minutes instead of asking users for screenshots. If authentication is needed, add -U username:password, but avoid saving passwords in shell history.

Basic curl proxy syntax

The standard form is simple:

curl -x protocol://proxy_host:proxy_port https://target.example.com

You can also use the longer option:

curl --proxy http://proxy.example.com:8080 https://target.example.com

The proxy address comes after -x. The site or API you want to reach comes last. That order matters. If you swap them, curl may treat the wrong value as the destination, which leads to confusing errors.

Common proxy formats include:

  • http://proxy.example.com:8080 for an HTTP proxy
  • https://proxy.example.com:8443 for a proxy that itself uses TLS
  • socks5://127.0.0.1:1080 for a SOCKS5 proxy
  • socks5h://127.0.0.1:1080 for SOCKS5 with DNS resolution through the proxy

The annoying part is that one missing scheme can cost ten minutes of pointless testing. Be explicit. Write http://, https://, or socks5h:// rather than relying on defaults.

Using an HTTP proxy

For many corporate networks, the proxy will be an HTTP proxy. This does not mean the final website must use HTTP. You can still request an HTTPS site through an HTTP proxy:

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

In this case, curl asks the proxy to open a tunnel to the HTTPS site. The encrypted connection is still between curl and the final server, unless your organization uses TLS inspection. If certificate errors appear only on the corporate network, TLS inspection may be the reason.

To see more detail, add -v:

curl -v -x http://proxy.company.local:8080 https://example.com

-v shows the connection steps, proxy negotiation, TLS handshake, response headers, and errors. It is the first option to use when a proxy command does not behave as expected.

Using a SOCKS proxy

SOCKS proxies are common in local tunnels, SSH forwarding, privacy tools, and testing setups. Use socks5:// when your machine should resolve DNS. Use socks5h:// when the proxy should resolve DNS.

curl -x socks5://127.0.0.1:1080 https://example.com

Or, with DNS handled by the proxy:

curl -x socks5h://127.0.0.1:1080 https://example.com

For location testing, socks5h is often safer. It prevents local DNS lookups from exposing the request path or causing a region mismatch. Honestly, it feels like a tiny detail until one DNS lookup sends you to the wrong CDN node.

Proxy authentication

If the proxy requires a username and password, use -U:

curl -x http://proxy.example.com:8080 -U alice:secret https://example.com

You may also see credentials placed inside the proxy URL:

curl -x http://alice:secret@proxy.example.com:8080 https://example.com

That works, but it is risky on shared systems. Shell history, process lists, CI logs, and monitoring tools may display the command. A safer method is to read the password from an environment variable or use a protected configuration file.

Some enterprise proxies use specific authentication schemes. curl supports options such as:

  • --proxy-basic for Basic authentication
  • --proxy-digest for Digest authentication
  • --proxy-ntlm for NTLM authentication
  • --proxy-negotiate for Negotiate authentication

Example with NTLM:

curl -x http://proxy.company.local:8080 --proxy-ntlm -U DOMAIN\\alice:password https://intranet.example.com

Set a proxy with environment variables

If you run many curl commands, typing -x every time gets old. You can set proxy environment variables instead:

export HTTP_PROXY=http://proxy.example.com:8080
export HTTPS_PROXY=http://proxy.example.com:8080

After that, curl will use the proxy automatically for matching requests:

curl https://example.com

To exclude hosts, set NO_PROXY:

export NO_PROXY=localhost,127.0.0.1,.internal.example.com

This tells curl to connect directly to local addresses and selected internal domains. It is useful when a proxy breaks access to development services or private network tools.

To bypass all proxy settings for one command, use:

curl --noproxy "*" https://example.com

Use a curl config file

For repeated work, create a curl config file. This keeps commands shorter and reduces typing errors.

proxy = "http://proxy.example.com:8080"
user-agent = "curl-proxy-test/1.0"
connect-timeout = 10

Save it as proxy.conf, then run:

curl --config proxy.conf https://example.com

You can also use the default curl config file at ~/.curlrc. Be careful with credentials. Set file permissions so other local users cannot read secrets:

chmod 600 ~/.curlrc

Confirm that the proxy is being used

Do not assume traffic is going through the proxy. Test it. A quick method is to request an IP-check service before and after adding the proxy:

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

If the second response shows the proxy’s public IP, the route is working. If both responses match, curl may be bypassing the proxy, the proxy may be local, or a network rule may be overriding the path.

For deeper checks, use verbose mode:

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

Look for lines showing a connection to the proxy host. You should see curl connect to the proxy first, not directly to the final site.

Common errors and fixes

  • Could not resolve proxy: The proxy hostname is wrong, DNS is failing, or the machine cannot reach the resolver.
  • Connection refused: The proxy host is reachable, but the port is closed or the proxy service is not running.
  • 407 Proxy Authentication Required: The proxy needs credentials. Add -U or the correct authentication option.
  • TLS certificate errors: The proxy or network may be inspecting HTTPS traffic. Install the approved CA certificate only if your security team authorizes it.
  • Slow requests: Add --connect-timeout 10 and --max-time 30 so scripts do not hang forever.

Example with time limits:

curl -x http://proxy.example.com:8080 --connect-timeout 10 --max-time 30 https://api.example.com

Security practices

Treat proxy access as sensitive. A proxy can see destination hosts, timing, headers, and, for plain HTTP, full content. Never send secrets through an unknown proxy. For production testing, use approved infrastructure and log only what you need.

Avoid putting passwords directly into scripts. Prefer environment variables, secret managers, or protected CI variables. Also review headers. Commands copied from browsers may include cookies, bearer tokens, or internal IDs.

Practical examples

Test an API through a corporate proxy:

curl -x http://proxy.company.local:8080 https://api.example.com/health

Send a POST request through a proxy:

curl -x http://proxy.example.com:8080 \
  -H "Content-Type: application/json" \
  -d '{"status":"ok"}' \
  https://api.example.com/events

Use a SOCKS5 tunnel created by SSH:

ssh -D 1080 user@jumpbox.example.com
curl -x socks5h://127.0.0.1:1080 https://internal.example.com

For reliable daily use, keep commands explicit. State the proxy protocol, host, port, authentication method, timeout, and target URL. That habit prevents vague failures and makes troubleshooting much faster when a proxy, firewall, or DNS rule changes without warning.