What is the equivalent of
curl -u username:password ...
in PowerShell\'s Invoke-RestMethod
? I tried this:
$sec
You basically need to pass the username and password pair to Invoke-RestMethod
as an encoded credentials variable.
What worked for me was the following:
$USERNAME = 'user'
$PASSWORD = 'password'
$IDP_URL = 'example.com/token'
$credPair = "$($USERNAME):$($PASSWORD)"
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($credPair))
$parameters = @{
Uri = $IDP_URL
Headers = @{ 'Authorization' = "Basic $encodedCredentials" }
Method = 'POST'
Body = '...'
ContentType = '...'
}
Invoke-RestMethod @parameters
Note how you can extract the request parameters into $parameters
to avoid bloating your command.