Solution: How to call Release Rest API from MSFT Powershell?

While using/testing REST API of Release with Postman or other tools, it works easy and fine. When I use powershell, I just get 401s Errors, why is that ?

Solution:
When talking to (api/v1)

  • Use Invoke-RestMethod
  • Attach a basic auth token to the Authorization header

But when talking to the Rest Extensions (api/extensions)

  • Use Invoke-RestMethod
  • I new create and use a PSCredential instead of appending a basic auth token

Powershell snippets:

api/extension

$encpwd = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($username, $encpwd)

$jiraResponse = Invoke-RestMethod -Uri $restApi -Method POST -Headers $headers -ContentType “application/json” -Body $body -Credential $cred

api/v1

$auth = “Basic " + [ System.Convert ]::ToBase64String([ System.Text.Encoding ]::UTF8.GetBytes((”{0}:{1}" -f $ username, $password)))

$headers = @{Authorization = $auth }

$response = Invoke-RestMethod -Uri $restApi -Method PUT -Headers $headers -ContentType “application/json” -Body $body

1 Like