Skip to content

Instantly share code, notes, and snippets.

@i-e-b
Created November 17, 2020 12:28
Show Gist options
  • Select an option

  • Save i-e-b/6c6a58f2aef91e4c1a96b20a28cdc5d7 to your computer and use it in GitHub Desktop.

Select an option

Save i-e-b/6c6a58f2aef91e4c1a96b20a28cdc5d7 to your computer and use it in GitHub Desktop.
Output error body from Powershell REST call
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept", "application/json")
$headers.Add("Authorization", "Bearer $($_MY_BEARER_TOKEN_)")
$headers.Add("Content-Type", "application/json")
try {
$reponse = Invoke-RestMethod $_MY_URL_ -Method Post -Body $_MY_PAYLOAD_STRING_ -UseBasicParsing -Headers $($headers)
# ... success, do stuff with response here ...
} catch {
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
$reader = New-Object -TypeName System.IO.StreamReader -ArgumentList ($_.Exception.Response.GetResponseStream())
while ( $read = $reader.ReadLine() ) {
Write-Host $read
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment