Đang đăng nhập từ app DHN Proxy...

How to Use a Proxy With cURL: HTTP, HTTPS & SOCKS Examples

Try DHN Proxy for free
How to Use a Proxy With cURL: HTTP, HTTPS & SOCKS Examples

Learn cURL proxy syntax for HTTP and SOCKS connections, authentication, environment variables and connection testing.

Using a proxy with cURL is one of the fastest ways to test a proxy endpoint, verify its public IP, troubleshoot authentication, or route command-line requests through another network.

For a basic HTTP proxy, use:

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

If the proxy requires authentication:

curl -x http://proxy.example.com:8080 \
  -U "username:password" \
  https://example.com

For SOCKS5 with proxy-side DNS resolution:

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

The general syntax is simple:

curl --proxy PROXY_URL TARGET_URL

This guide covers the cURL proxy commands developers use most often, including HTTP, HTTPS, SOCKS5, authentication, environment variables, IP verification, troubleshooting, and security tips.

How Do You Use a Proxy With cURL?

Diagram showing how cURL sends a request through a proxy to a website or API and receives a response

The standard cURL proxy syntax is:

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

The shorter version is:

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

In this command:

  • curl sends the request;

  • -x or --proxy defines the proxy;

  • http://proxy.example.com:8080 is the proxy endpoint;

  • https://example.com is the destination.

cURL first connects to the proxy and then sends traffic toward the destination through that connection.

For most use cases, this is all you need to start testing a curl proxy.

Read more >> Vietnam Proxy: Residential, Mobile & ISP Guide

Use an HTTP Proxy With cURL

For a standard HTTP proxy:

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

The same HTTP proxy can normally be used when the target website uses HTTPS:

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

The proxy protocol and destination protocol are separate.

This command still uses an HTTP proxy even though the destination starts with https://.

When accessing an HTTPS destination through an HTTP proxy, cURL commonly uses the HTTP CONNECT method to create a tunnel before starting the encrypted connection to the destination.

To inspect the connection process, add verbose mode:

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

Verbose output can help diagnose:

  • proxy connection failures;

  • DNS problems;

  • CONNECT responses;

  • TLS issues;

  • HTTP status codes;

  • authentication errors.

Be careful when sharing verbose output because it may contain headers, tokens, cookies, or other sensitive information.

Use an HTTPS Proxy

An HTTPS destination does not mean the proxy itself must use HTTPS.

This command:

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

uses an HTTP connection to the proxy.

If the proxy endpoint itself supports TLS, use:

curl -x https://proxy.example.com:8443 https://example.com

In that case, the connection between cURL and the proxy is encrypted as well.

If an HTTPS proxy does not work as expected, check your installed cURL version:

curl --version

This helps confirm the version and supported protocols in your cURL build.

Proxy Authentication With cURL

Proxy authentication with cURL using username and password before connecting to a website or API

Commercial proxies commonly require a username and password.

The preferred option is --proxy-user, or its shorter form -U.

curl \
  -x http://proxy.example.com:8080 \
  -U "username:password" \
  https://example.com

You can also write it on one line:

curl -x http://proxy.example.com:8080 -U "username:password" https://example.com

Credentials may also be included directly in the proxy URL:

curl -x http://username:password@proxy.example.com:8080 https://example.com

However, using -U is generally easier to read and reduces the chance of credentials being copied inside URLs.

If your password contains shell-sensitive characters such as $, !, &, or spaces, quote it carefully.

For example:

curl \
  -x http://proxy.example.com:8080 \
  -U 'username:my$complex!password' \
  https://example.com

Exact quoting behavior depends on the shell you use.

Use a SOCKS5 Proxy With cURL

cURL also supports SOCKS proxies.

A basic curl SOCKS5 proxy command is:

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

You can also use:

curl --socks5 proxy.example.com:1080 https://example.com

There is an important difference between socks5:// and socks5h://.

With:

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

the destination hostname is normally resolved locally.

With:

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

the destination hostname is resolved through the proxy.

For remote proxy workflows, socks5h is often preferable when you want DNS resolution to happen through the proxy connection.

The equivalent long-form command is:

curl --socks5-hostname proxy.example.com:1080 https://example.com

For SOCKS5 authentication:

curl \
  -x socks5h://proxy.example.com:1080 \
  -U "username:password" \
  https://example.com

Read more >> SOCKS5 vs HTTP Proxy: Differences, Speed, Security & Use Cases

HTTP vs SOCKS5 Proxy in cURL

The best protocol depends on the task.

Proxy type

Example

Typical use

HTTP

http://host:port

Standard HTTP and HTTPS requests

HTTPS proxy

https://host:port

Encrypted connection to the proxy

SOCKS5

socks5://host:port

SOCKS connection with local DNS

SOCKS5H

socks5h://host:port

SOCKS connection with proxy-side DNS

For normal web requests and API calls, HTTP proxies are usually sufficient.

SOCKS5 is useful when you need SOCKS support or want more control over where DNS resolution occurs.

Set a cURL Proxy With Environment Variables

If you use the same proxy repeatedly, environment variables can save you from adding -x to every command.

For HTTP traffic:

export http_proxy="http://proxy.example.com:8080"

Then:

curl http://example.com

For HTTPS destinations:

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

Then:

curl https://example.com

For SOCKS or a general proxy:

export ALL_PROXY="socks5h://proxy.example.com:1080"

You can also exclude selected hosts with NO_PROXY:

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

For one request that should bypass any configured proxy:

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

One cURL-specific detail is worth remembering: the http_proxy environment variable should normally be lowercase.

For production scripts and CI pipelines, avoid hard-coding proxy passwords directly into source files. Use your platform's secret-management tools where possible.

Verify That cURL Is Using the Proxy

A simple way to test a proxy is to compare your direct public IP with the IP returned through the proxy.

First, run:

curl https://api.ipify.org

Then run:

curl \
  -x http://proxy.example.com:8080 \
  https://api.ipify.org

If authentication is required:

curl \
  -x http://proxy.example.com:8080 \
  -U "username:password" \
  https://api.ipify.org

For SOCKS5:

curl \
  -x socks5h://proxy.example.com:1080 \
  https://api.ipify.org

If the proxy is working, the proxied request should normally return the public IP used by the proxy connection instead of your direct public IP.

For JSON output:

curl "https://api.ipify.org?format=json"

This is useful when testing proxies inside scripts.

Common cURL Proxy Errors

407 Proxy Authentication Required

A 407 response generally means the proxy requires authentication or rejected the supplied credentials.

Check the username, password, hostname, port, and account status.

Use verbose mode for troubleshooting:

curl -v \
  -x http://proxy.example.com:8080 \
  -U "username:password" \
  https://example.com

Could Not Resolve Proxy

If cURL reports that it could not resolve the proxy, verify the proxy hostname.

For example:

Could not resolve proxy

This usually indicates a hostname typo or DNS problem.

Connection Timeout

A timeout can result from an incorrect port, blocked connection, unavailable endpoint, firewall rule, or routing issue.

Set a connection timeout while testing:

curl \
  --connect-timeout 10 \
  -x http://proxy.example.com:8080 \
  https://example.com

You can also limit the entire request:

curl \
  --max-time 30 \
  -x http://proxy.example.com:8080 \
  https://example.com

Proxy CONNECT Failed

An HTTPS request through an HTTP proxy may fail during the CONNECT stage.

Run:

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

Then inspect the CONNECT response.

Possible causes include a wrong endpoint, unsupported destination port, proxy policy, or unavailable proxy server.

SOCKS Proxy Does Not Work

Make sure the proxy protocol is correct.

These commands are not equivalent:

curl -x http://proxy.example.com:1080 https://example.com
curl -x socks5h://proxy.example.com:1080 https://example.com

If the endpoint is SOCKS5, the scheme should reflect that.

Security Tips for cURL Proxy Connections

Proxy credentials should be treated like other infrastructure credentials.

Avoid storing usernames and passwords in public repositories, shared scripts, screenshots, or logs.

You can separate credentials into environment variables:

export PROXY_HOST="proxy.example.com:8080"
export PROXY_USER="username"
export PROXY_PASS="password"

Then:

curl \
  -x "http://$PROXY_HOST" \
  -U "$PROXY_USER:$PROXY_PASS" \
  https://example.com

Environment variables are convenient, but they are not always the safest option in production. CI systems, containers, and cloud platforms usually provide dedicated secrets management.

Also avoid using:

-k

or:

--insecure

as a default fix for TLS errors.

These options disable normal certificate verification and can weaken connection security. Investigate the certificate, proxy, DNS, or TLS configuration instead.

Quick cURL Proxy Commands

Task

Command

HTTP proxy

curl -x http://host:port https://example.com

Proxy auth

curl -x http://host:port -U "user:pass" https://example.com

SOCKS5

curl -x socks5://host:port https://example.com

SOCKS5 with proxy DNS

curl -x socks5h://host:port https://example.com

Verify proxy IP

curl -x http://host:port https://api.ipify.org

Verbose debugging

curl -v -x http://host:port https://example.com

Connection timeout

curl --connect-timeout 10 -x http://host:port https://example.com

Bypass proxy

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

For most authenticated HTTP proxy setups, this template is enough:

curl \
  --proxy http://PROXY_HOST:PROXY_PORT \
  --proxy-user "USERNAME:PASSWORD" \
  https://example.com

For SOCKS5 with proxy-side DNS:

curl \
  --proxy socks5h://PROXY_HOST:PROXY_PORT \
  --proxy-user "USERNAME:PASSWORD" \
  https://example.com

Try Your Proxy Endpoint

Before adding a proxy to a script, scraper, application, or QA workflow, test the endpoint directly with cURL.

Start with:

curl \
  -x http://PROXY_HOST:PROXY_PORT \
  -U "USERNAME:PASSWORD" \
  https://api.ipify.org

Then test the real website or API your application needs to access.

Check whether:

  • authentication succeeds;

  • the connection is stable;

  • the public IP changes as expected;

  • the proxy location is correct;

  • latency is acceptable;

  • the destination responds normally.

If you need a proxy endpoint for testing, you can review the available options at DHN Proxy and insert the provided host, port, username, and password into the commands above.

Testing the endpoint with cURL first makes later application integration much easier to diagnose.

Frequently Asked Questions

How do you use a proxy with cURL?

Use the -x or --proxy option:

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

For authentication:

curl -x http://proxy.example.com:8080 \
  -U "username:password" \
  https://example.com

How do I use an HTTP proxy with cURL?

Use an HTTP proxy URL:

curl --proxy http://host:port https://example.com

The destination can still use HTTPS.

How do I use a SOCKS5 proxy with cURL?

Use:

curl -x socks5://host:port https://example.com

For proxy-side DNS resolution:

curl -x socks5h://host:port https://example.com

What is the difference between socks5 and socks5h?

With socks5://, destination DNS resolution generally happens locally. With socks5h://, the hostname is sent through the proxy for resolution.

How do I authenticate to a cURL proxy?

Use -U or --proxy-user:

curl \
  -x http://host:port \
  -U "username:password" \
  https://example.com

How can I check whether the proxy is working?

Request a public IP service through the proxy:

curl -x http://host:port https://api.ipify.org

Compare that result with the IP returned when you make the same request without the proxy.

Can cURL use proxy environment variables?

Yes. Common variables include http_proxy, HTTPS_PROXY, ALL_PROXY, and NO_PROXY.

Why does cURL return 407 Proxy Authentication Required?

A 407 response means proxy authentication is required or the credentials were rejected. Verify the username, password, endpoint, port, and account configuration.

Does cURL support both HTTP and SOCKS proxies?

Yes. cURL supports HTTP proxies and SOCKS variants. Use the correct scheme such as http://, socks5://, or socks5h:// so cURL knows how to connect.

Should I use --insecure if HTTPS fails through the proxy?

Not as a general solution. --insecure disables certificate verification. Troubleshoot the TLS, certificate, proxy, or DNS configuration first.