Created
November 17, 2020 12:28
-
-
Save i-e-b/6c6a58f2aef91e4c1a96b20a28cdc5d7 to your computer and use it in GitHub Desktop.
Output error body from Powershell REST call
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $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