BudiBadu Logo
Samplebadu

cURL by Example: Proxy Request

Latest

Route requests through a proxy server. Supports HTTP, HTTPS, and SOCKS proxies.

Code

# Use an HTTP proxy
curl -x http://proxy.example.com:8080 https://target.com

# Proxy with authentication
curl -x http://user:[email protected]:8080 https://target.com

# Use a SOCKS5 proxy (common for Tor or SSH tunnels)
curl --socks5 localhost:9050 https://check.torproject.org

# Bypass proxy for specific hosts
curl --noproxy localhost,127.0.0.1 https://localhost/api

Explanation

Proxies are intermediary servers that route your network traffic, acting as a gateway between your client and the destination server. cURL provides comprehensive support for various proxy types including HTTP, HTTPS, and SOCKS proxies. The -x (or --proxy) flag is the primary mechanism for configuring proxy settings. The syntax is protocol://host:port, for example -x http://proxy.example.com:8080. Proxies serve multiple purposes: corporate networks often require all traffic to flow through a proxy for security monitoring and access control, developers use proxies for debugging and traffic inspection with tools like Charles or Fiddler, privacy-conscious users route traffic through SOCKS proxies (like Tor) to anonymize their connections, and geo-restricted content can sometimes be accessed through proxies located in different regions.

Proxy Authentication: Many corporate and commercial proxies require authentication. You can provide credentials in two ways: embedded directly in the proxy URL using the format -x protocol://username:password@host:port, or separately using the -U (or --proxy-user) flag like -U username:password. Embedding credentials in the URL is convenient but can expose passwords in process listings or logs. The -U flag is slightly more secure as it separates authentication from the connection details. For maximum security in scripts, consider reading credentials from environment variables or secure vaults rather than hardcoding them.

SOCKS Proxies: SOCKS (Socket Secure) is a protocol that routes network packets between client and server through a proxy server. SOCKS proxies operate at a lower level than HTTP proxies, making them protocol-agnostic—they can handle any type of traffic, not just HTTP/HTTPS. The --socks5 flag enables SOCKS version 5 proxy support: --socks5 localhost:9050 (commonly used with Tor). The difference between socks5:// and socks5h:// protocols is crucial: socks5h:// performs DNS resolution through the proxy, which is essential for privacy as it prevents DNS leaks that could reveal your browsing to your ISP even when using a proxy. SOCKS proxies are particularly common in privacy tools (Tor network), SSH tunneling scenarios, and bypassing restrictive firewalls since they work at the transport layer.

Bypass Proxy for Specific Hosts: The --noproxy option accepts a comma-separated list of hostnames or domains that should bypass the proxy and be accessed directly. For example: --noproxy "localhost,127.0.0.1,.local". This is crucial for local development where services running on localhost or your LAN should not be routed through an external proxy. You can use wildcards like *.internal.company.com to exclude entire domains. In corporate environments with system-wide proxy settings, this flag is essential for accessing local development servers, internal company resources, or preventing unnecessary proxy overhead for local services. The special value "*" disables the proxy completely for that request, overriding any system or environment proxy settings.

Code Breakdown

2
-x sets the proxy address.
5
Inline credentials user:pass@host are supported.
8
--socks5 is specific for SOCKS v5 proxies.
11
--noproxy is crucial for local development when a system-wide proxy is set.