Proxies are intermediary servers between clients like your computer and other servers. They can be used to cache data, filter web content, bypass region restrictions, and many more.

Some of the popular proxy types are HTTP and SOCKS, and cURL has built-in support for both proxy types. cURL also supports options for HTTP and SOCKS proxies such as basic authentication.

You can configure cURL to connect via HTTP or SOCKS proxy directly from the command line. You can also use the configuration file or the environment variable for a more permanent solution.

Steps to use HTTP or SOCKS proxy in cURL:

  1. Open the terminal.
  2. Use cURL with an HTTP proxy using the -x flag:
    $ curl -x http://proxy-server:port https://www.example.com/

    The -x flag followed by the proxy server's URL and port number tells cURL to route its request through that proxy.

  3. To use SOCKS5 with a hostname resolution through the proxy, utilize the –socks5-hostname flag:
    $ curl --socks5-hostname socks5-server:port https://www.example.com/

    This approach ensures the DNS resolution happens through the proxy and not locally, providing an extra layer of privacy.

  4. For a more persistent proxy setting, you can set the http_proxy environment variable.
    $ export http_proxy=http://proxy-server:port
    $ curl https://www.example.com/

    Remember, if you set the proxy using environment variables, all cURL requests from that session will go through the proxy unless overridden with direct command flags.

  5. For HTTPS requests through a proxy, you can also set the https_proxy environment variable.
    $ export https_proxy=http://proxy-server:port
    $ curl https://www.secure-example.com/
  6. To make the proxy setting persistent for all cURL sessions, add the proxy configuration to the cURL config file.
    $ echo "proxy = http://proxyserver:port" >> ~/.curlrc

    Keep in mind that this method applies the proxy setting to every cURL session initiated by the user, unless overridden by command-line flags.

  7. Test the configuration by making a request to a known server.
    $ curl https://www.example.com/

    Expected outcome would be to see the server's response as if accessed through the specified proxy.

Discuss the article:

Comment anonymously. Login not required.