In this article, I am going to show you how to use a proxy with cURL.
Contents
Getting started
A proxy server acts as a gateway between your computer and the internet. It may provide better privacy, security, and performance.
cURL supports both HTTP and SOCKS proxies. These proxies can work with HTTP, HTTPS, and FTP protocols out of the box.
To specify a proxy that you want to use with cURL, you need to use the -x
or --proxy
option followed by the proxy address which is in the form of protocol://host:port
.
Example #1: Naked proxy
I use the term naked proxy
to refer to proxies that do not require any authentication methods.
To get the main page from https://example.com
via the HTTP proxy at 111.111.111.111:8080
:
$ curl -x http://111.111.111.111:8080 https://example.com
Similarly, with the SOCKS5 proxy at 222.222.222.222:3333
:
$ curl -x socks5://222.222.222.222:3333 https://example.com
To get the main page from https://protection.com
which requires basic authentication, you need to use the -u
option followed by the credential which is in the form of user:password
:
$ curl -x http://111.111.111.111:8080 -u user:password https://protection.com
$ curl -x socks5://222.222.222.222:3333 -u user:password https://protection.com
To read more about basic authentication, you can read the article 3 methods to use basic authentication with cURL.
Example #2: Protected Proxy
I use the term protected proxy
to refer to proxies that require basic authentication to protect itself from abuse.
cURL provides the -U
or --proxy-user
option to let you specify the credential of a proxy server.
Okay, you can give it a try:
$ curl -U proxy_user:proxy_password -x http://111.111.111.111:8080 https://example.com
You can even use a protected proxy to access a protected destination resource:
$ curl -U proxy_user:proxy_password -x http://111.111.111.111:8080 -u user:password https://protection.com
Conclusion
Using a proxy with cURL is super simple. cURL also supports the environment variables such as http_proxy
and HTTPS_PROXY
, but I do not like this method so I do not introduce them in this article.