Proxy locations

Europe

North America

South America

Asia

Africa

Oceania

See all locations

Network statusCareers

Back to blog

How to Use cURL With Proxy?

How to Use cURL With Proxy
Iveta Vistorskyte

Iveta Vistorskyte

2024-03-187 min read
Share

This step-by-step guide will explain how to use curl, or simply curl, with proxy servers. It covers all the aspects, beginning with installation and explaining various options for setting the proxy.

We didn’t target specific proxy services, so this tutorial should work with all proxy servers. All you need to know is the proxy server details and credentials. When it comes to Oxylabs’ proxy servers, you can easily use curl with Residential, Datacenter Proxies, Web Unblocker, and other proxy server types.

This is a fairly technical tutorial and expects readers to have a basic understanding of proxy servers. It would be especially interesting and useful for those starting with web scraping.

What is cURL?

curl is a command line tool for sending and receiving data using the URL. Let’s look at the simplest example of using curl. Open your terminal or command prompt, type in this curl command, and press Enter:

curl https://www.google.com
Link to GitHub

The above curl command will get the HTML of the page and print it in the console:

Checking the HTML output of google.com via curl

If you add a -I tag at the end of the command:

curl https://www.google.com -I

It’ll print the headers of the HTTP response:

Checking the HTTP response headers via curl

The question “what is curl?” is also answered in one of our previous articles. We recommend reading it if you want to learn how it became such a universal asset.

Installation

curl command line tool is provided with many Linux distributions and with macOS. It also comes with Windows 10, officially starting from version 1804 and up. You can check whether your computer has curl installed by opening your terminal and running the following command:

curl --version

You should see a similar output with the curl version and its release date as highlighted in green:

Checking the curl version via the terminal

If curl isn’t found, you can install it yourself.

Windows

Download curl for Windows machines from curl.se/windows. Make sure to choose the file with the appropriate architecture for your PC.

macOS

For macOS computers, it’s recommended to use a package manager like Homebrew. Once you install Homebrew, you can use it to install curl:

brew install curl

Linux

If your Linux distribution isn’t provided with curl, you can install it by running the install command. For example, on Ubuntu or Debian, open Terminal and run this command:

sudo apt install curl
Link to GitHub

What you need to connect to a proxy

Irrespective of which proxy provider you use, you’ll need the following information to anonymize curl behind proxy IPs:

  • proxy server address

  • port

  • protocol

  • username (if authentication is required)

  • password (if authentication is required)

In this tutorial, we’re going to assume that the proxy server is 127.0.0.1, the port is 1234, the username is user, and the password is pwd. We’ll look into multiple examples covering various protocols. Bear in mind that curl supports different connection protocols, including HTTP, HTTPS, SOCKS4, SOCKS5, and many more, which you can read about here.

NOTE. If you’re on a network that uses NTLM authentication, you can use the switch --proxy-ntlm while running curl commands. Similarly, --proxy-digest can be used for digest authentication. You can look at all the available command line options by running curl --help. This tutorial will have examples of scenarios when a username and password have to be specified.

The next section will cover the first curl proxy server scenario, which happens to be the most common one – HTTP and HTTPS proxy with curl.

Using cURL with HTTP/HTTPS proxy

If you recall, we looked at using curl without proxy like this:

curl "https://ip.oxylabs.io/"

This particular website is especially useful for testing out proxies, as the output of this page is the origin IP address. If you’re using a proxy correctly, the page will return an IP address that’s different from your machine’s, that is, the proxy server IP address.

There are multiple ways to run curl with proxy command. The next section will cover sending proxy details as a command line argument. Moreover, we'll explore how to integrate HTTPS proxies. We’ve overviewed curl on various aspects in other posts, so feel free to check them out if you want to learn how to send curl GET and curl POST requests, as well as how to send HTTP headers with curl.

NOTE. All the command line options, or switches, are case sensitive. For example, -f instructs curl to fail silently, while -F denotes a form to be submitted.

Command line argument to set proxy in cURL

Open the terminal, type the following command, and press Enter:

curl --help all
Link to GitHub

The output is going to be a huge list of options. One of them is going to look like this:

-x, --proxy [protocol://]host[:port] 
Link to GitHub

Note that x is lowercase, and it’s case-sensitive. The proxy details can be supplied using -x or --proxy switch. Both of the curl with proxy commands mean the same thing:

curl -x "http://user:pwd@127.0.0.1:1234" "https://ip.oxylabs.io/"
Link to GitHub

or

curl --proxy "http://user:pwd@127.0.0.1:1234" "https://ip.oxylabs.io/"
Link to GitHub

NOTE. If there are SSL certificate errors, add -k (note the lowercase k) to the curl command. This will allow insecure server connections when using SSL.

curl --proxy "http://user:pwd@127.0.0.1:1234" "https://ip.oxylabs.io/" -k
Link to GitHub

You may have noticed that both the proxy url and target url are surrounded in double quotes. This is a recommended practice to handle special characters in the url.

Another interesting thing to note here is that the default proxy protocol is HTTP. Thus, the following two commands will do exactly the same:

curl --proxy "http://user:pwd@127.0.0.1:1234" "https://ip.oxylabs.io/"
curl --proxy "user:pwd@127.0.0.1:1234" "https://ip.oxylabs.io/"
Link to GitHub

If you want to use curl through proxy running the HTTPS protocol, you can specify the curl HTTPS protocol by setting the proxy address to https://. The full string would then be https://user:pwd@127.0.0.1:1234:

curl --proxy "https://user:pwd@127.0.0.1:1234" "https://ip.oxylabs.io/"

Using environment variables

Another way to use proxy with curl is to set the environment variables http_proxy and https_proxy

Note that setting proxy using environment variables works only with MacOS and Linux. For Windows, see the next section which explains how to use the _curlrc file.

If you look at the first part of these variable names, it clearly shows the protocol for which these proxies will be used. It has nothing to do with the protocol used for the proxy server itself.

  • http_proxy – the proxy will be used to access URLs that use the HTTP protocol

  • https_proxy – the proxy will be used to access URLs that use the HTTPS protocol

Simply set the variables http_proxy to the HTTP proxy address and https_proxy to set the HTTPS proxy address. Open terminal and run these two commands.

export http_proxy="http://user:pwd@127.0.0.1:1234"
export https_proxy="http://user:pwd@127.0.0.1:1234"
Link to GitHub

After running these two commands, run curl normally. The commands below will use the http_proxy to connect to the target URL starting with http://, while the https_proxy will be used to connect to the target starting with https://

curl "http://ip.oxylabs.io/"
curl "https://ip.oxylabs.io/"
Link to GitHub

If you see SSL Certificate errors, add -k to ignore these errors.

Another thing to note here is that these curl proxy settings apply system-wide. If this behavior isn’t desired, turn off the global proxy by unsetting these two variables:

unset http_proxy
unset https_proxy
Link to GitHub

See the next section to set the default proxy only for curl and not system-wide.

Configure cURL to always use proxy

If you want a proxy for curl but not for other programs, this can be achieved by creating a curl config file.

For Linux and MacOS, open the terminal and navigate to your home directory. If there’s already a .curlrc file, open it. If there’s none, create a new file. Here are the set of commands that can be run:

cd ~
nano .curlrc
Link to GitHub

In this curl configuration file, add proxy authorization line:

proxy="http://user:pwd@127.0.0.1:1234"
Link to GitHub

Save the config file. Now, curl with proxy is ready to be used. Simply run curl normally, and it will read the proxy from the .curlrc file.

curl "https://ip.oxylabs.io/"
Link to GitHub

On Windows, the file is named _curlrc. This file can be placed in the %APPDATA% directory.

To find the exact path of %APPDATA%, open command prompt and run the following command:

echo %APPDATA%
Link to GitHub

This directory will be something like C:\Users\<your_user>\AppData\Roaming. Now go to this directory, create a new file called _curlrc, and set the proxy by adding this line:

proxy="http://user:pwd@127.0.0.1:1234"
Link to GitHub

This works exactly the same way in Linux, MacOS, and Windows.

Ignore or override proxy for one request

Ignore or override proxy for one request

If the proxy is set globally or by modifying the .curlrc file, this can still be overridden to set another proxy or even bypass it.

To override the proxy for one request, set the new proxy using -x or --proxy switch as usual:

curl --proxy "http://user:pwd@1.0.0.1:8090" "https://ip.oxylabs.io/"
Link to GitHub

If you want to bypass proxy altogether for a request, you can use the curl noproxy command by passing --noproxy followed by “*”. This instructs curl to not use proxy for all URLs.

curl --noproxy "*" "https://ip.oxylabs.io/"
Link to GitHub

If you have many curl requests to execute without a proxy but you don’t want to change global proxy settings, the following section will show you exactly how to do that.

Bonus tip – turning proxies off and on quickly

This tip is dedicated only for advanced users. If you do not know what a .bashrc file is, you may skip this section.

You can create an alias in your .bashrc file to set proxies and unset proxies. For example, open the .bashrc file using any editor or terminal and add these lines:

alias proxyon="export http_proxy=' http://user:pwd@127.0.0.1:1234';export https_proxy=' http://user:pwd@127.0.0.1:1234'"
alias proxyoff="unset http_proxy;unset https_proxy"
Link to GitHub

After adding these lines, save the .bashrc and update the shell to read this .bashrc. To do this, run this command in the terminal:

Now, whenever you need to turn on the proxy, you can quickly turn on the proxy, run one or more curl commands, and then turn off the proxies like this:

proxyon
curl "http://ip.oxylabs.io/"
curl "https://ip.oxylabs.io/"
proxyoff  
Link to GitHub

cURL socks proxy

If you want to use a SOCKS proxy, the syntax remains the same:

curl -x "socks5://user:pwd@127.0.0.1:1234" "https://ip.oxylabs.io/"
Link to GitHub

Similarly, socks4://, socks4a://, socks5:// or socks5h:// can be used depending on the SOCKS version your proxy supports.

Alternatively, curl socks proxy can also be set using the switch --socks5 instead of -x. You can follow the same command but use a different switch: username and password can be sent using the --proxy-user switch.

curl --socks5 "127.0.0.1:1234" "https://ip.oxylabs.io/" --proxy-user user:pwd

Again, --socks4, --socks4a, --socks5, or --socks5-hostname can be used, depending on the version.

This tutorial is also available in a video format, so take a look if you prefer visual guides:

Summary

curl is a very powerful tool for automation and is arguably the best command line interface in terms of proxy support. Lastly, as libcurl works very well with PHP, many web applications use it for web scraping projects, making it a must-have for any web scraper. Furthermore, its versatility extends to various use cases, such as using curl with APIs and even leveraging curl in Python. Be sure to explore these articles for further insight.

Click here and check out a repository on GitHub to find the complete code used in this article. You can also learn more on web scraping using Selenium and some other useful libraries like Beautiful Soup or lxml tutorial in our blog.

Frequently Asked Questions

What is proxy in curl?

A proxy server acts as an IP address that curl can connect to and send web requests through. If no proxy server is used via curl, then it uses the IP address the computer is connected to, such as the home Wi-Fi network. Once you set up curl with a proxy server, curl will instead use the IP address of the proxy, overcoming any network restrictions and IP bans.

What is the difference between wget and curl proxy?

While Wget proxy settings and curl proxy server settings don’t differ too much other than their syntax, both tools are quite similar yet different. In short, curl is more versatile and offers more features, while Wget is straightforward and best for downloading files and mirroring websites. This is because Wget can download resources recursively, meaning it can follow and download from every link on a web page.

What is the default port of curl proxy?

When you don’t include the proxy port number, for example, http://user:pwd@127.0.0.1, then curl will use a default port in the proxy address, depending on the specified proxy protocol:

  • HTTP1080

  • SOCKS41080

  • SOCKS51080

  • HTTPS443

What is the no_proxy variable in curl?

The no_proxy variable in curl allows you to specify domain suffixes, IP addresses, or host names that shouldn’t be accessed via a proxy server.

For instance, when you enter export no_proxy="ip.oxylabs.io" in the terminal, it ensures that no proxy will be used when accessing any page on the ip.oxylabs.io domain. Once that’s set, any curl requests to ip.oxylabs.io will use the computer’s IP address.

You can clear your no_proxy settings by running unset no_proxy in your terminal.

About the author

Iveta Vistorskyte

Iveta Vistorskyte

Lead Content Manager

Iveta Vistorskyte is a Lead Content Manager at Oxylabs. Growing up as a writer and a challenge seeker, she decided to welcome herself to the tech-side, and instantly became interested in this field. When she is not at work, you'll probably find her just chillin' while listening to her favorite music or playing board games with friends.

All information on Oxylabs Blog is provided on an "as is" basis and for informational purposes only. We make no representation and disclaim all liability with respect to your use of any information contained on Oxylabs Blog or any third-party websites that may be linked therein. Before engaging in scraping activities of any kind you should consult your legal advisors and carefully read the particular website's terms of service or receive a scraping license.

Related articles

Get the latest news from data gathering world

I’m interested

IN THIS ARTICLE:


  • What is cURL?


  • Installation


  • What you need to connect to a proxy


  • Using cURL with HTTP/HTTPS proxy


  • Command line argument to set proxy in cURL


  • Using environment variables


  • Configure cURL to always use proxy


  • Ignore or override proxy for one request


  • Bonus tip – turning proxies off and on quickly


  • cURL socks proxy


  • Summary

Web Scraper API for block-free data harvesting

Extract quality data from any website hassle-free while avoiding CAPTCHA and IP blocks.

Scale up your business with Oxylabs®