How do I use Curl with proxy settings?

Using Curl with proxy settings is a common practice to bypass network restrictions and access web pages anonymously.

To use Curl with proxy settings, you can use the -x or --proxy option followed by the proxy address.

Here is the basic syntax:

curl -x [protocol://][user:password@]proxyhost:proxyport URL
  • protocol: The protocol of the proxy server, it can be either HTTP or SOCKS.
  • user and password: Credentials for the proxy server if it requires authentication.
  • proxyhost and proxyport: The hostname or IP address and the port number of the proxy server.
  • URL: The URL that you want to access via the proxy server.

For example, to send a request to http://example.com via an HTTP proxy server running on localhost port 8080, you can use the following command:

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

If the proxy server requires authentication, you can provide the username and password like this:

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

You can also use a SOCKS proxy with Curl. For a SOCKS4 proxy, use socks4:// as the protocol:

curl -x socks4://localhost:1080 http://example.com

For a SOCKS5 proxy, use socks5:// as the protocol:

curl -x socks5://localhost:1080 http://example.com

You can also instruct Curl to use the proxy for all requests by setting the http_proxy or all_proxy environment variable:

export http_proxy=http://localhost:8080
curl http://example.com

This will route all HTTP traffic through the proxy server at localhost:8080.

Note that some versions of Curl may have been compiled without proxy support. You can check if your version of Curl supports proxies by running curl -V (capital V) and looking for HTTPS-proxy or SOCKS in the output.

curl -V

If your version of Curl doesn't support proxies, you'll need to install a version that does or use another tool that supports proxies, such as wget or httpie.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon