curled plant stem

This article describes how to install and use CURL on Windows. Curl is a free command line utility used for transferring files using various protocols. Most commonly it is used for transferring files over HTTP(S).

cURL comes natively installed on Unix based operating systems such as MacOS and Linux. But windows is left out. Now that we have PowerShell on windows, you can get some of the functionality of cURL using various cmdlets like invoke-webrequest. However, if you are used to using a Unix toolset, you will be left wondering where you can find cURL.

Methods of Installing cURL

To install cURL on windows, you have five real options:

  • Install using Chocolatey ( Windows Package manager)
  • Download pre-compiled Binaries
  • Compile from source code
  • Install Cygwin
  • Install Windows Subsystem for Linux (WSL)

In the next few sections, we will discuss each of the above installation methods.

Chocolatey Package Manager

If you want to go the Chocolatey route, it is really simple, just run:

choco install curl -y

 This will install cURL for you. However, you need to first install the Chocolatey package manager. You can find instructions on how to install Chocolatey here.

Download Pre-Compiled cURL Binaries

cURL is supported on many platforms. And if you navigate here, you will see binaries you can download for many different platforms. All the way at the bottom of the page you will find the windows builds. They are all labeled as Win32 or Win64

I recommend you download the zip file in the Win64 – Generic section:

curl download screenshot

After you have downloaded and extracted the zip file, look in the src folder, you will find curl.exe. Copy curl.exe into your C:\windows\system32\ folder. I choose this folder because it includes all of the other system utilities. And it is already part of your PATH variable, so you don’t have to do anything to add it.

Compile from Source Code

This is the hardest method of installation. If you are trying to get things up quickly, you should skip over this section. But, for those who want to do it just for the experience, or those who absolutely need the latest version, building from source code might be the method for you. You can find instructions on how to install from source code here: https://curl.haxx.se/docs/install.html

Install CYGWIN

CYGWIN is a Unix-like environment for Windows. You can’t natively run all of your favorite Unix tools on Windows. First, you have to download the source code and compile it to run on windows. Just like in the previous section. CYGWIN is a project where someone has gone through the work to pre-compile many of your favorite Unix tools for you.

To use cURL as part of CYGWIN, you first download and CYGWIN installer from here. While you are going through the installer, you will encounter a list of packages you wish to install. Make sure you select the cURL package.

Install Windows Subsystem for Linux (WSL)

Starting with Windows 10, Microsoft has released a product called Windows Subsystem for Linux (WSL). WSL gives you the bash command shell on windows, which is the same shell that runs on Linux/Unix. Installing WSL gives you a full Linux environment, unlike CYGWIN which is simply a collection of Unix utilities. WSL is based on Ubuntu Linux. So you will have tools like apt-get to install programs etc…. The main limitation of WSL is you don’t have a GUI. But that is fine for our purposes today.

Older builds of Windows 10 will have to enable Developer mode before they can install the windows feature. You can find instructions on how to do that here.

Starting with the windows 10 Fall Creators update, you don’t have to enable Developer mode. However, it is still a two-step process. First, you enable the windows feature. Then you install the Linux distribution from the windows store. instructions can be found here.

After you have installed WSL, go to your Start menu, click on Bash, and you can use all of your favorite Linux tools, including cURL.

How to use cURL on Windows

As this guide is about installing cURL, I won’t go into great detail about using curl. However, here are some basics.  Each of these commands will largely work the same way on both Windows and Unix variants.

First, you need to launch the appropriate command line environment. If you installed WSL or CYGWIN, you will need to go to your start menu and launch CYGWIN or BASH. Otherwise, you can launch a command prompt or Powershell window.

Next, type:

curl —help

The above command will show you all the different command arguments you can pass to cURL. This will be very important as you are learning how to use the tool.

Lets try pulling down the text of a webpage using curl with the following command:

curl http://54.184.76.36

You should see all the HTML code from idkrtm.com scroll by…that means it is working.

How to download website headers with curl

Next, let’s just pull down the headers. This is useful for just checking if a web page is working. If you’re on Linux, OSX/MacOS, CYGWIN, or WSL, the command would be:

curl -s -D - http://54.184.76.36 -o /dev/null

the output should  be similar to this:

HTTP/1.1 200 OKDate: Mon, 12 Feb 2018 15:25:35 GMTServer: ApacheX-Powered-By: PHP/5.5.38Link: ; rel=”https://api.w.org/”, ; rel=shortlinkTransfer-Encoding: chunkedContent-Type: text/html; charset=UTF-8

On Windows Powershell, the same command would be:

curl -s -D - http://54.184.76.36 -o $null

Or on Windows CMD, the same command would be:

curl -s -D – http://54.184.76.36 -o nul

cURL Post request

When you make post requests, there are three kinds of posts you will probably make

  1. Post Variables
  2. Post a file
  3. Post JSON

For these three operations, we will need the -d, -x and -h CLI arguments. Here is an example of how we would post variables

curl -d "key1=value1&key2=value2" -X POST http://localhost

In the above example, we start by calling the curl command, then using the -d argument for data. Within the quotes, we pass in the 2 URL parameters we want to pass to the server. We then use the -X argument to tell cURL that we are going to make a post. Then we pass in the URL of the server we want to post to.

In the next example, we will post a file to the same URL

curl -d "@myfile.txt" -X POST http://localhost

This example is very similar to the original example. We use the same -d and -X arguments. But within the quotes we used @myfile.txt . This will upload myfile.txt from the current working directory and post it to the URL we specified

In the next example, we will pass in JSON to the same endpoint. You will notice in this example, we are passing in the -H parameter.

curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost

You can see this is very similar to the previous two examples. However, we passed in JSON within the quotes, and we added a Content-Type:application/json header to the input. We did not have to do this in the previous examples, because the other examples were able to use the default content type: “Content-Type: application/x-www-form-urlencoded”

As you can imagine, the JSON you pass in can get quite large, so you can also pass in a JSON file

curl -d "@myfile.json" -X POST http://localhost

cURL Post request

As you are interacting with websites, it is only a matter of time before you encounter one where you need to authenticate. Similar to how you authenticate in your browser. cURL supports a variety of authentication methods, but today we will only cover basic authentication, which is very easy. See the example below.

curl -u myusername:mypassword http://localhost

In the above example, we added a -u parameter for username. We then separated the username from the password with a colon.

Summary

Thank you for stopping by today. We discussed five different ways to install cURL on your windows computer. And some of the most common commands you might need to use with cURL.

As you can see, cURL is a very flexible tool that can work on both Windows and Unix variants of operating systems. If you want to know a lot more about cURL, there is a free book on GitHub, called Everything Curl, that goes into great detail about everything you want to know about cURL.