How to Use cURL With Proxy

Use this detailed guide complete with code snippets to help jump start your cURL with proxies journey.
12 min read
How to Use cURL With Proxy blog image

In this post, we will cover:

What Is cURL?

cURL, short for “Client URL,” is a tool to send and receive data over the Internet using URLs. As a software project, it provides both a library (libcurl) and a command-line tool (curl). Here, we will focus on curl, the command-line tool for transferring data via the URL syntax. 

The project supports a wide range of protocols, including HTTP, HTTPS, FTP, FTPS, SFTP, POP3, POP3S, IMAP, IMAPS, LDAP, LDAPS, SCP, and more. This makes curl the most popular and widely used command-line HTTP client in the world. 

cURL is highly versatile and represents a common solution to perform various tasks, such as making HTTP requests, downloading files, uploading data, and interacting with APIs. It offers several features, from request customization to proxy support.

Installing curl

Let’s now learn how to install curl on your machine.

macOS

You do not need to install curl on macOS. The tool is already included in the operating system, and you can use it natively in the Terminal application.

Windows

Starting with Windows 10, Windows comes with a copy of curl. At the same time, the curl command is an alias for the PowerShell Invoke-WebRequest command. This means that curl commands in the Windows terminal will invoke Invoke-Request behind the scenes. To avoid this and actually use curl from cURL, replace “curl” with “curl.exe.” This way, PowerShell will run curl and not Invoke-Request.

For example, you can verify the current version of curl installed on your Windows machine in the terminal with:

curl.exe --version

That should print something similar to:

curl 8.0.1 (Windows) libcurl/8.0.1 Schannel WinIDN

Release-Date: 2023-03-20

Protocols: dict file ftp ftps http https imap imaps pop3 pop3s smtp smtps telnet tftp

Features: AsynchDNS HSTS HTTPS-proxy IDN IPv6 Kerberos Largefile NTLM SPNEGO SSL SSPI threadsafe Unicode UnixSockets

If you are a Windows user, replace all “curl” instances in the commands of the article with the “curl.exe” string. Otherwise, set up the Windows Subsystem for Linux (WSL) and follow the instructions below.

Linux

On Linux, the procedure for installing curl changes depending on the specific distribution you are using. Popular Linux distributions, such as Ubuntu and Fedora, come with it by default. So, you can use curl directly in the terminal.

In other distributions, curl may not be included. In this case, you can add it using the distribution package manager. On a Debian-based OS, you can install curl with the following command:

sudo apt-get install curl

What You Need to Use a Proxy in curl

A proxy acts as an intermediary between the client and the destination server. It intercepts requests from the client, forwards them to the server, receives the response from the server, and sends it back to the client. This middle-man approach increases anonymity and helps avoid network restrictions. This is because the destination server will see the requests as coming from the IP and location of the chosen proxy server and from you.

To get started using curl with proxy services, you first need access to a proxy. Specifically, here is the syntax of a proxy URL:

[<PROTOCOL>://][<USERNAME>:<PASSWORD>]@<HOST>[:<PORT>]

This consists of:

  • <PROTOCOL>: The protocol to use to connect to the proxy server. If no protocol is specified, curl will default to http://.
  • <HOST>: The required IP address or URL of the proxy server’s hostname.
  • <PORT>: The port number that the proxy server is listening to. If no port is specified, curl will use 1080 by default.
  • <USERNAME>: The optional username to specify when authentication is required.
  • <PASSWORD>: The optional password to specify when authentication is required.

As for proxy protocols, the most popular ones are HTTP and HTTPS, followed by SOCKS.

It is time to retrieve a valid HTTP proxy!

You can get one for free on Free Proxy List, as below:

IP Address: 71.19.249.97; Port: 8443

This means the proxy URL is:

http://71.19.249.97:8443

Opting for such a solution is okay for learning purposes, but you cannot rely on it in a real-world scenario. Free proxy services are unreliable, error-prone, slow, data-greedy, and short-lived. Do not use them!

The solution? Premium proxies from Bright Data, the best provider in the market. Subscribe and try our reliable proxies for free.

Keep in mind that premium solutions are protected by authentication. Suppose the protocol is HTTP, host is 71.103.203.109, and port is 9321, and the pair of credentials are admin-32458 and rTuJ3tEwf. In this case, the curl proxy URL would be:

http://admin-32458:@rTuJ3tEwf71.103.203.109:9321

How to Specify an HTTP/HTTPS Proxy in curl

Before getting started, launch the command below in your terminal:

curl "https://httpbin.org/ip"

As you can see, the basic curl syntax is:

curl [optional_commands] <URL>

A curl best practice recommends surrounding URLs with double quotes to avoid issues with special characters.

Run it and you should get something like this:

{

  "origin": "194.33.243.7"

}

The HTTPBin project allows you to get information about your HTTP requests. In detail, the /ip endpoint returns the origin IP of the request, which is the IP address of the machine that performed the request. In other words, it returns your IP. 

After setting up a proxy in curl, you should see the IP address of the proxy server and not yours.

There are several ways to set a proxy in curl. Let’s now jump into the most popular approaches to specifying an HTTP/HTTPS proxy in curl.

Using a Command Line Argument

curl offers two command line arguments to set a proxy URL:

  • -x
  • --proxy

The two can be used interchangeably, as they do exactly the same thing. The latter is just an alias of the former.

So, the syntax to use curl with proxy services becomes:

curl -x [<PROTOCOL>://][<USERNAME>:<PASSWORD>]@<HOST>[:<PORT>] <URL>

Or:

curl --proxy [<PROTOCOL>://][<USERNAME>:<PASSWORD>]@<HOST>[:<PORT>] <URL>

Note: Command line options are case-sensitive in curl. For example, -x and -X have two different meanings.

If you now launch:

curl -x "http://71.19.249.97:8443" "https://httpbin.org/ip"

Or the equivalent:\

curl --proxy "http://71.19.249.97:8443" "https://httpbin.org/ip"

You should get:

{

  "origin": "71.19.249.97"

}

The origin matches the proxy server IP. This means that the target site sees the request as coming from the proxy, as desired. Great!

Do not forget that free proxies are short-lived, so the above server is unlikely to still be alive by the time you read this article. In case of an error, replace it with a fresh proxy.

Using Environment Variables

Another way to use a proxy in curl is by setting the following two environment variables:

  • http_proxy:The URL of the proxy server to use to access URLs involving the HTTP protocol.
  • https_proxy: The URL of the proxy server to use to access URLs involving the HTTP protocol.

On macOS and Linux, you can set those two environments with the syntax below:

export http_proxy="[<PROTOCOL>://][<USERNAME>:<PASSWORD>]@<HOST>[:<PORT>]"

export https_proxy="[<PROTOCOL>://][<USERNAME>:<PASSWORD>]@<HOST>[:<PORT>]"

So, the two commands will become:

export http_proxy="http://71.19.249.97:8443"

export https_proxy="http://71.19.249.97:8443"

On Windows, you need to use this PowerShell syntax:

$env:http_proxy = "[<PROTOCOL>://][<USERNAME>:<PASSWORD>]@<HOST>[:<PORT>]"

$env:https_proxy = "[<PROTOCOL>://][<USERNAME>:<PASSWORD>]@<HOST>[:<PORT>]"

Then, in our example:

$env:http_proxy = "http://71.19.249.97:8443"

$env:https_proxy = "http://71.19.249.97:8443"

From now on, every curl command will go through the specified proxies without having to use the -x option:

Again, this will return:

curl https://httpbin.org/ip

{

  "origin": "71.19.249.97"

}

To turn off the curl proxies, unset the environment variables with:

unset http_proxy

unset https_proxy

Or on Windows:

$env:http_proxy = ""

$env:https_proxy = ""

curl will go back to its standard behavior and https://httpbin.org/ip will now return your IP.

Using a configuration file

You can globally specify a proxy for curl by creating a .curlrc config file.

On Linux and macOS, open the terminal and navigate to your home directory:

cd ~

Then, access the .curlrc with nano:

nano .curlrc

If the file already exists, it will be opened. Otherwise, it will be created first and then opened.

Add the following line to the .curlrc file:

proxy="http://71.19.249.97:8443"

The syntax of this instruction is:

proxy="[<PROTOCOL>://][<USERNAME>:<PASSWORD>]@<HOST>[:<PORT>]"

Save the file, close the terminal and relaunch it. 

On Windows, you need to create a _curlrc file inside the %APPDATA% directory with the same content. Paste %APPDATA% in the file explorer address bar and press ENTER to access the directory. 

This should bring you to C:\Users\<YOUR_USER>\AppData\Roaming.

Now, curl will automatically use the proxy specified in the configuration file:

curl "http://httpbin.org/ip"

You will get:

{

  "origin": "71.19.249.97"

}

Fantastic! You just learned how to use curl with proxy servers in three different ways!

How To Set SOCKS Proxies in curl

If you want to use a SOCKs proxy, the command structure remains the same:

curl -x [<PROTOCOL>://][<USERNAME>:<PASSWORD>]@<HOST>[:<PORT>] <URL>

The main difference is that <PROTOCOL> will be socks4, socks4a, socks5, or socks5h, instead of http and https.

A complete example of a curl SOCKS proxy in action is:

curl -x "socks5://94.14.109.54:2478" "http://httpbin.org/ip"

As you can expect, the result will be:

{

  "origin": "94.14.109.54"

}

An alternative approach involves the --socks4, --socks4a, --socks5 command-line options instead of -x. In this case, you must set the proxy URL without username and password after the option and then the credentials after --proxy-user:

curl --socks4|--socks4a|--socks5 <HOST>[:<PORT>] <URL> --proxy-user <USERNAME>:<PASSWORD>

For example:

curl --socks4 "93.16.238.41:8721" "http://httpbin.org/ip" --proxy-user admin334:kv4NsDgc3

This will connect to HTTPBin via the 93.16.238.41:8721 socks4 proxy, using admin334 as the username and kv4NsDgc3 as the password for authentication.

Tips and Tricks You Should Know

In this section, you will see some interesting tricks and valuable tips for dealing with a curl proxy like a pro.

Quickly Turning Proxies ON and OFF

The idea here is to define aliases to programmatically set and unset proxies via environment variables with custom commands. 

On macOS and Linux, you can do that by creating a .bashrc file. This is a script file that is executed every time the user logs in.

Move to your home folder:

cd ~

Then, create or open a .bashrc file:

nano .bashrc

Add the content in the format below to the file:

alias proxyon="export http_proxy='[<PROTOCOL>://][<USERNAME>:<PASSWORD>]@<HOST>[:<PORT>]';export https_proxy='[<PROTOCOL>://][<USERNAME>:<PASSWORD>]@<HOST>[:<PORT>]'"
alias proxyoff="unset http_proxy;unset https_proxy"

Thus, in our example it would be:

alias proxyon="export http_proxy='http://71.19.249.97:8443';export https_proxy='http://71.19.249.97:8443'"

alias proxyoff="unset http_proxy;unset https_proxy"

Save the file and restart your machine.

Now, you can activate and deactivate a curl proxy with:

proxyon

curl "http://httpbin.org/ip"

# other HTTP requests...

proxyoff

Note the custom proxyon and proxyoff commands. These perform the operations specified by the aliases in the .bashrc file.

On Windows, you can achieve something similar with the PowerShell aliases.

Ignoring Proxies for a Single Request

If you want to prevent curl from using a configured proxy for a specific request, you can use the --noproxy "*” argument as below:

curl --noproxy "*" <URL>

This instructs curl not to use proxies for all URLs.

Set up a proxy and run:

curl --noproxy "*" "https://httpbin.org/ip"

The resulting origin will contain your IP, not that of the proxy server.

Avoiding SSL Certificate Errors

When using proxies in curl, your requests may fail because of an SSL certificate errors. To avoid that, use the -k option as in the syntax below:

curl -x [<PROTOCOL>://][<USERNAME>:<PASSWORD>]@<HOST>[:<PORT>] -k <URL>

-k helps you avoid certificate errors by allowing insecure server connections over SSL.

For example, you can contact HTTPBin bypassing SSL issues with:

curl -x "http://71.19.249.97:8443" -k "https://httpbin.org/ip"

Getting More Information About the Request

When a request in curl fails, it is not easy to understand what happened. To diagnose the error and see all the operations performed by curl, set the -v option:

curl -x [<PROTOCOL>://][<USERNAME>:<PASSWORD>]@<HOST>[:<PORT>] -v <URL>

This will output the details about the connections established and the headers used by the tool:

$curl -x "http://71.19.249.97:8443" -v "https://httpbin.org/ip"

*   Trying 71.19.249.97:8443...

* Connected to 71.19.249.97 (71.19.249.97) port 8443 (#0)

* allocate connect buffer

* Establish HTTP proxy tunnel to httpbin.org:443

> CONNECT httpbin.org:443 HTTP/1.1

> Host: httpbin.org:443

> User-Agent: curl/8.0.1

> Proxy-Connection: Keep-Alive

>

< HTTP/1.0 200 OK

<

* CONNECT phase completed

* CONNECT tunnel established, response 200

* schannel: disabled automatic use of client certificate

* ALPN: offers http/1.1

* ALPN: server accepted http/1.1

* using HTTP/1.1

> GET /ip HTTP/1.1

> Host: httpbin.org

> User-Agent: curl/8.0.1

> Accept: */*

>

< HTTP/1.1 200 OK

< Date: Sat, 26 Aug 2023 10:39:13 GMT

< Content-Type: application/json

< Content-Length: 31

< Connection: keep-alive

< Server: gunicorn/19.9.0

< Access-Control-Allow-Origin: *

< Access-Control-Allow-Credentials: true

<

{

  "origin": "71.19.249.97"

}

* Connection #0 to host 71.19.249.97 left intact

Which Proxies Are Best for curl?

The answer to this question depends on your use case and the nature of the curl requests you have to make. To find the right solution for your needs, take a look at the different types of proxies available:

  • Datacenter proxies: They are fast but may be detected and blocked easily by sites due to their identifiable IP ranges.
  • Residential proxies: They guarantee a high level of anonymity because they offer IP addresses coming from real devices in specific locations. Residential proxies are perfect for accessing sites relying on geo-restriction blocks or anti-bot measures, and to perform web scraping with curl.
  • ISP proxies: They are fast, secure, and highly reliable as they provide IPs from devices registered to ISPs. ISP proxies represent a perfect solution for SEO monitoring, web browsing, and market research.
  • Mobile proxies: They offer IPs from real mobile devices for a high level of anonymity. They are useful for accessing mobile-specific applications, sites, or content.

This was just a brief recap, but you can find out more in our guide to proxy IP types.

Conclusion

In this curl proxy guide, you learned what cURL is, what it has to offer, and how to set up an HTTP/HTTPS/SOCKS proxy. As it turns out, you can’t rely on curl with proxy services from free providers. Thus, the main technical decision to make is which proxy provider to adopt. Save time and energy and go for the best on the market, Bright Data.

Bright Data controls the best proxy servers in the world, serving Fortune 500 companies and over 20,000 customers. Its worldwide proxy network involves:

Overall, that is one of the largest and most reliable scraping-oriented proxy networks on the market. Join the largest proxy network and get a free trial.

No credit card required