-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Please explain how to get the response body when an exception is thrown. #4456
Comments
|
You have to wrap it in a try/catch block. You may have to dig a few layers deep in the exception to find the root cause. |
|
@markekraus Is the body of the response available in the exception record? |
|
@sdwheeler that code will output the code and the (generic) error message that gets printed to the console. For a 400 error for example, that will get you "Bad Request" But REST APIs generally send additional data in the body, which is what @hroe is referring to. The returned data is in the $error but you have to work to get at it: |
|
@FISHMANPET @sdwheeler, Thanks, Herman |
|
for what it's worth, after doing some more digging, I discovered that in PS6 that data is properly read and included directly in the error, so you only need to do that to get the error data on 5.1 and below. |
|
@FISHMANPET is right: If JSON response is: You could get the message as follows: |
# '20220421': try/catch rest method call, read addtional data, see MicrosoftDocs/PowerShell-Docs#4456
Thank you @FISHMANPET , it worked. |
|
Nowadays this is even easier. in 7.4 you can use irm -Method Post http://localhost:8000/api/example -Err Fail
|| ConvertFrom-Json $Fail.Message |
We need this for better error handling for POST requests to https://api.powerbi.com/v1.0/myorg/groups.
For http 400 errors, Fiddler revealed that response body has useful explanation, such as:
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: