I am writing a Powershell client to invoke a REST API as follows:
$response = try {
(Invoke-WebRequest $endpoint_url `
-Method Post `
-Headers $endpoint_header `
-ErrorAction Stop)
} catch [System.Net.WebException] {
Write-Host "A web exception was caught: $($_.Exception.Message)"
Write-Host $_.Exception.StackTrace
Write-Host $response.StatusCode.Value__ $response.StatusDescription
}
Write-Host $response.StatusCode.Value__ $response.StatusDescription
Write-Host $response.RawContent
Write-Host $response.Content
The API returns a response when it sends a 500 Internal Server Error. I wish to capture the response in the script.
The statement
Write-Host $response.Content
was something that I was hoping would work. But it works only if the API returns a 200 OK. Is there some this something I would need to do to capture this?
Thanks,
Prabal
1