Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell's Invoke-RestMethod equivalent of curl -u (Basic Authentication)

What is the equivalent of

curl -u username:password ... 

in PowerShell's Invoke-RestMethod? I tried this:

$securePwd = ConvertTo-SecureString "password" -AsPlainText -Force $credential = New-Object System.Management.Automation.PSCredential ($username, $securePwd)  Invoke-RestMethod -Credential $credential ... 

but it returns 401, Unauthorized.

like image 296
Borek Bernard Avatar asked Jul 10 '14 09:07

Borek Bernard


1 Answers

This is the only method that worked for me so far:

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))  Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} ... 

But I don't believe there isn't a better way.

like image 50
Borek Bernard Avatar answered Sep 22 '22 00:09

Borek Bernard