Invoke-RestMethod Basic Auth with Null Password

Using PowerShell Core is it possible to pass basic auth. credentials with a null password?

I am trying to interact with a documented API that uses an API key as the username for basic authentication, the password is null.

They provide a curl example as:

curl -u "$API_KEY:"
"Note the colon after $API_KEY which prevents cURL from asking for a password."

In PS Core I have tried using Get-Credential to pass in the username and null password, but this doesn’t seem to work, my assumption is the password is empty rather than null.

I can work around this by using the old fashioned way of constructing credentials:

[pre]$auth = @{Authorization = 'Basic ’ + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(“$($Apikey):”))}[/pre]



You can try building credential using [PSCredential]::New() method and by creating empty SecureString (System.Security.SecureString)

$Username = 'blalba'
$EmptySecureString = [System.Security.SecureString]::New()
$Credential = [PScredentia;]::New($Username,$EmptySecureString)

PS: I never used this approach before.


Thank you, that worked perfectly and I think looks a bit more elegant and easier to understand than my previous legacy attempt.