First of all, what is cURL? To pull directly from their website :
curl is an open source command line tool and library for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMTP, SMTPS, Telnet and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, cookies, user+password authentication (Basic, Plain, Digest, CRAM-MD5, NTLM, Negotiate and Kerberos), file transfer resume, proxy tunneling and more.
More simply stated, cURL is a tool that is used at the command line for transferring data.
So now what if you want to use it in PowerShell? If you are using version 5.x of PowerShell you may think that you already have it. You can check your version of PowerShell by calling the command : $PSVersionTable.PSVersion
Using cURL in PowerShell
You can type in a cURL command like one that downloads a file from a GitHub repository.
curl http://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/mongodb-on-ubuntu/azuredeploy.json > azuredeploy3.json
and it will seem like it works but what it is actually doing is just using cURL as an alias. In the above instance, what will happen is that you will just get the headers instead of the file itself. Alias’ in PowerShell allow you to create shortcuts for longer commands so you don’t have to type them out all of the time. If you type in the command Get-Alias, it will give you a list of all the Alias’ that are used in PowerShell. As you can see, the curl command just calls the Invoke-WebRequest command. They are similar but not the same which is why the above request does not work for us.
To get this to work in PowerShell you would need to change the syntax slightly.
( file name cut off in image “https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/mongodb-on-ubuntu/azuredeploy.json”)
You then open the file from PowerShell either using Code or Sublime.
Utilizing the switches that Invoke-WebRequest uses instead of cURL does the trick. But if you want to stay consistent and use the same commands on your MAC as you do on your PC, you will need to remove the alias and install cURL on the PC in order to achieve this.
The first thing you need to do is remove the alias.
Removing the cURL alias
If you don’t already have a profile, run the following command to create one.
New-Item $profile -force -itemtype file
Next, open up the profile file. (This will open the file up in notepad)
add the following line to the profile file.
save the notepad file and close it.
Then either run the following command to reload the profile OR close and reopen Powershell to refresh.
Now if you run the Get-Alias command again, you will see that the curl alias is gone.
Installing cURL on your PC
There are a number of ways you can do this. One way is to use the cURL for Windows created by the Confused By Code author : http://www.confusedbycode.com/curl/ this is a step by step installer that installs curl, adds it to your path, installs the certs etc.. but in keeping with my new mindset of doing everything from the command line that I can, I decided to install it using Chocolately. https://chocolatey.org/
Chocolatey is a Linux style command line packet manager for windows. I will be using it in the future so it makes sense to use it for this. To install it do the following
Open up PowerShell in Admin Mode and run the following command.
iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
Once you have installed Chocolatey, you can then use it to install curl. (Open and close PowerShell first)
choco install curl
Now you have curl installed but if you try to run the curl command from above, you will receive some OpenSSL errors. To fix this you have two choices, One use the -R switch to ignore the certs (not recommended) OR install the certs.
To install the certs, navigate to C:\ProgramData\chocolatey\lib\curl\tools ( do it in windows explorer or powershell, doesn’t matter to me)
Double click mk-ca-bundle.vbs, or if you have Perl, mk-ca-bundle.pl. The script will download a new SSL bundle from Mozilla. It will be called ca-bundle.crt. once it is downloaded rename the file to curl-bundle.crt and leave it in the same directory.
Open and close PowerShell and then run the following command.
curl https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/mongodb-on-ubuntu/azuredeploy.json > azuredeploy6.json
(By the way, this is an ARM template that I am using to install MongoDB in Azure on Ubuntu if you are curious. See my Mean On Azure Series if you want to learn more. http://sogeek.us/meanonazure)
That’s all.. I hope you found that useful.
Daniel – The Sociable Geek