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

后端 未结 8 1447
长发绾君心
长发绾君心 2020-11-28 06:41

What is the equivalent of

curl -u username:password ...

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

$sec         


        
8条回答
  •  感情败类
    2020-11-28 07:24

    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.

提交回复
热议问题