There's no good reason I can think of to want to use it in CMD.
There is however a good reason to want to use it in Windows, without the need for cygwin for instance.
You can do this in PowerShell.
First of all
The -u
option in cURL
is not portable. You should understand it's a cURL
specific feature where it will try a number of common authentication types. In this case, it is basic. So, for good practise, in your scripts and programs, you should use -H "Authorization: Basic <base64-Data>
For instance;
If my username was myname@domain.com
, and my password was mypassword
, the format would be like this:
To make a base64 header, is as simple as typing into your GNU terminal:
echo -n 'myname@domain.com:mypassword' | openssl base64 -base64
Or by using PowerShell builtins:
[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("myname@domain.com:mypassword")))
Secondly
You can make a template for this in windows using PowerShell ISE.
For instance, in ServiceM8, this would list all of your clients:
$user = 'username@domain.com'
$pass = 'myPassword'
$method = 'GET'
$base64Creds = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(($user+":"+$pass)))
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", 'application/json')
$headers.Add("Authorization", $base64Creds)
$response = Invoke-RestMethod 'https://api.servicem8.com/api_1.0/company.json' -Method $method -Headers $headers
Write-Output $response
You can post data by changing the $method
to POST and adding a content body, like mentioned here:
Put your parameters in a hash table and pass them like this:
$postParams = @{username='me';moredata='qwerty'} Invoke-WebRequest
-Uri http://example.com/foobar -Method POST -Body $postParams
In PowerShell you could access the properties of the output object. $response.name
, $response.billing_address
, $response.uuid
, etc..
If you absolutely must use CMD, then I'd suggest wrapping up the above into a ps1
file and executing it from your batch script using powershell -executionPolicy bypass -file "C:\Users\Whatever\MyCmd.ps1"