-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Allow Web Cmdlets to Ignore HTTP Error Statuses #10466
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
Conversation
The -SkipHttpErrorCheck flag causes web request cmdlets (Invoke-WebRequest and Invoke-RestMethod) to ignore HTTP statuses that are error statuses and treat them as successful requests. This allows users to handle the responses using their own error handler and gives them access to the full, unmodified response body and headers.
This allows the user to specify a variable to set to the integer value of the respons's status code, Analogous to using -ResponseHeadersVariable to retrieve the headers of the response. This can be used to distinguish error messages from success messages when used with -SkipHttpErrorCheck.
The summary for the SkipHttpErrorCheck property didn't conform to style guidelines. This changes the summary to start with "Gets or sets".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes look mostly good. Please add test coverage per conversation in PowerShell bridge.
This flag supresses terminating errors on web cmdlets. The tests are written to check that it properly supressed the errors.
Th -StatusCodeVariable parameter specifies an output variable for the status code with Invoke-RestMethod. This test makes sure it functions properly.
|
I've added the tests, as requested. |
test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved pending minor fixes suggested by @iSazonov
Variables had different cases from each other and some parameter names were lower case.
Variables had different cases from each other and some parameter names were lower case.
These tests ensure that Web Cmdlets fail when -SkipHttpErrorCheck is missing.
test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
Outdated
Show resolved
Hide resolved
|
|
||
| $uri = Get-WebListenerUrl -Test 'Response' -Query $query | ||
| Invoke-RestMethod -SkipHttpErrorCheck -StatusCodeVariable code -Uri "$uri" | ||
| $code | Should -Be 200 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add test for status codes other than 200?
At least one 400 and 500?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a 404 and a 500 test. Let me know if you meant 400 specifically and not just any 4XX-range status.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant just anything in the range
Per discussion on the pull requests. This commit fixes up style problems with the tests for -SkipHttpErrorCheck.
Previously, the -StatusCodeVariable flag in Invoke-RestMethod only had tests for 200 status. This commit adds tests for 404 and 500 statuses and removes -SkipHttpErrorCheck from the 200 check.
The test was copy/pastes from the 200 status test. The body indicated success. This commit fixes that so it is also an error indicator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good pending @adityapatwardhan updated review
|
Is there anything else I need to do to get this done? |
|
@adityapatwardhan Can you update your review? |
|
@TravisEz13 I have signed off, sorry for the delay. |
|
@vdamewood Thanks for your contribution! |
|
🎉 Handy links: |
PR Summary
Fix #5555
Fix #9769
This PR adds a -SkipHttpErrorCheck flag to Invoke-WebRequest and Invoke-RestMethod, and adds a -ResponseStatusVariable option to Invoke-RestMethod. The -SkipHttpErrorCheck flag causes the cmdlets to ignore HTTP error statuses and continue to process the responses and write to the pipeline as if these had been successful. The -ResponseStatusVariable assigns the integer value of the status code to the variable named. It can be used with or without -SkipHttpErrorCheck.
PR Context
These cmdlets as they are currently written will throw an exception when the HTTP status is not considered a success. The error handling code will extract the body of the response and modify its contents to assign to the exception, but will leave the original body contents inaccessible. This isn't feasible with web APIs and other sites that return useful diagnostic messages in the body that would be modified by the error handling code.
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.