8000 Allow Web Cmdlets to Ignore HTTP Error Statuses by vdamewood · Pull Request #10466 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@vdamewood
Copy link
Contributor
@vdamewood vdamewood commented Aug 30, 2019

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

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.
@msftclas
Copy link
msftclas commented Aug 30, 2019

CLA assistant check
All CLA requirements met.

The summary for the SkipHttpErrorCheck property didn't conform
to style guidelines. This changes the summary to start with
"Gets or sets".
Copy link
Contributor
@markekraus markekraus left a 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.

@ghost ghost added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Aug 30, 2019
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.
@ghost ghost removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Aug 30, 2019
@vdamewood
Copy link
Contributor Author

I've added the tests, as requested.

@iSazonov iSazonov added CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Documentation Needed in this repo Documentation is needed in this repo labels Aug 30, 2019
@iSazonov iSazonov added this to the 7.0.0-preview.4 milestone Aug 30, 2019
Copy link
Contributor
@markekraus markekraus left a 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.
@ghost ghost added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Sep 3, 2019

$uri = Get-WebListenerUrl -Test 'Response' -Query $query
Invoke-RestMethod -SkipHttpErrorCheck -StatusCodeVariable code -Uri "$uri"
$code | Should -Be 200
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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.
@ghost ghost removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Sep 6, 2019
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.
Copy link
Member
@TravisEz13 TravisEz13 left a 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

@vdamewood
Copy link
Contributor Author

Is there anything else I need to do to get this done?

@TravisEz13
Copy link
Member

@adityapatwardhan Can you update your review?

@adityapatwardhan
Copy link
Member

@TravisEz13 I have signed off, sorry for the delay.

@TravisEz13 TravisEz13 merged commit 04a1fc3 into PowerShell:master Nov 7, 2019
@iSazonov
Copy link
Collaborator
iSazonov commented Nov 8, 2019

@vdamewood Thanks for your contribution!

@vdamewood vdamewood deleted the fix/iwr-irm branch November 10, 2019 20:48
@ghost
Copy link
ghost commented Nov 21, 2019

🎉v7.0.0-preview.6 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invoke-RestMethod does not return response headers Original Response Body Unattainable from Web Cmdltes on Non-Success Status Codes

6 participants

0