The command-line device cURL (consumer URL) is usually used to ship or obtain knowledge to a server. The device’s benefit is that it helps a number of protocols and presents options like authentication, proxy, switch resume, and extra.
We are able to additionally use cURL in PowerShell, however there are a couple of necessary issues that that you must know. And PowerShell additionally has it’s personal strategies to switch knowledge to and from servers.
On this article, I’ll clarify how you need to use cURL in PowerShell, and provide you with some examples.
PowerShell cURL
cURL has been round since 1997 and is used to work together with net providers, to ship or retrieve knowledge. The device is usually utilized in scripting or to automate duties. Naturally, you may wish to use cURL in your PowerShell scripts as effectively.
Now earlier than we are able to use cURL in PowerShell there may be one necessary factor that that you must know. As much as PowerShell 6, the command cURL has been an alias in PowerShell for the cmdlet Invoke-WebRequest
. This triggered many points/confusions, so since PowerShell 6, the command cURL now truly calls the curl.exe utility.
We are able to see this by wanting up the command with the Get-Command
cmdlet in each PowerShell 5 and PowerShell 7:
As you possibly can see, the command curl name the curl.exe in PowerShell 7, identical to we anticipate. However in PowerShell 5.1, the command is definitely an alias.
The issue with that is that the entire syntax of the Invoke-WebRequest
cmdlet is totally different from cURL, so your instructions gained’t work as you anticipate.
cURL is put in on Home windows 10/11 by default, however in case you don’t have cURL put in, then you possibly can obtain it right here.
Eradicating the Alias cURL
If you wish to use the true cURL command-line utility in PowerShell 5.1, then you might have two choices. You should use curl.exe as a command, or take away the alias. I favor the latter as a result of this may guarantee that your scripts additionally work on PowerShell 7. To take away the alias you need to use the next command:
Take away-Merchandise alias:curl
Utilizing cURL in PowerShell
So let’s check out some examples of the way to use cURL in PowerShell. Within the instance under I’ll present the true cURL technique and the PowerShell model, Invoke-WebRequest
to offer you a greater understanding of the variations.
Primary GET Request
To easily get the contents of a particular URL, we are able to use the instructions under.
curl https://httpbin.org/ip
Invoke-WebRequest -Uri "https://httpbin.org/ip" # On this case it is higher to make use of, as a result of the output is in JSON Invoke-RestMethod https://httpbin.org/ip
Sending Knowledge
The httpbin endpoint that I’m utilizing within the examples under will simply return the info that we ship to it, however that is good on this instance. To ship knowledge with cURL, we have to set the HTTP technique to POST.
curl -X POST https://httpbin.org/something -d "take a look at=knowledge"
The Invoke-WebRequest technique makes use of the identical precept, solely with a distinct syntax:
$physique = "take a look at=knowledge" Invoke-WebRequest -Uri "https://httpbin.org/something" -Technique POST -Physique "take a look at=knowledge"
Within the examples above we solely ship a single worth to the endpoint, however extra frequent is to submit a type or ship a number of fields to the endpoint. In cURL we are able to do that by specifying the fields utilizing the -F parameter.
# POST type knowledge with a number of fields curl -X POST https://httpbin.org/publish ` -F "identify=John Doe" ` -F "e mail=john@instance.com" ` -F "message=Hi there World" # Or for a JSON payload curl -X POST https://httpbin.org/publish -H "Content material-Sort: software/json" ` -d '{"field1":"value1","field2":"value2","field3":"value3"}'
When utilizing the Invoke-WebRequest
technique, we are able to first create a hashtable with the formdata that we wish to publish. And to maintain the code readable, we are able to additionally use a hashtable and splatting to retailer all of the parameters
$formData = @{ identify = "John Doe" e mail = "john@instance.com" message = "Hi there World" } $publish = @{ Uri = "https://httpbin.org/publish" Technique = 'Submit' Physique = $formData } Invoke-WebRequest @publish
Utilizing Authentication
Most APIs require a type of authentication to have the ability to switch knowledge with it. On the subject of authentication there are totally different strategies, however primary authentication (username & password) or bearer token authentication are essentially the most generally used choices.
To make use of primary authentication with cURL, you’ll need to move the username and password within the format username:passwor
d. Within the instance under we’ll add the username and password as effectively within the URL, however that’s just for testing functions:
curl -u "username:password" https://httpbin.org/basic-auth/username/password # returns on succesfull authentication { "authenticated": true, "person": "username" }
When utilizing the PowerShell technique, Invoke-WebRequest
, we first must create a credential object. You’ll be able to ask the person to enter the credentials, with the Get-Credential
cmdlet, however while you wish to retailer the credentials in your script, you’ll need to transform them to a safe string first:
# Get the credentials from the person $Credentials = Get-Credential -UserName "username" -Message "Enter your password" # Or retailer the credentials within the script $Password = ConvertTo-SecureString "password" -AsPlainText -Drive $Credentials = New-Object System.Administration.Automation.PSCredential ("username", $Password) Invoke-WebRequest -Uri "https://httpbin.org/basic-auth/username/password" -Credential $Credentials
Wrapping Up
It’s a superb factor that Microsoft eliminated the cURL alias in PowerShell 7, as a result of eventhough cURL and Invoke-WebRequest might look related, cURL has much more capabilites. In case you are creating scripts which can be going to run on different machines, then you need to use the command curl.exe
to make sure that cURL is used as a substitute of Invoke-WebRequest.
Hope you preferred this text, if in case you have any questions, simply drop a remark under.