-
Notifications
You must be signed in to change notification settings - Fork 7.1k
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
Add ResponseHeadersVariable Parameter to Invoke-RestMethod #4888
Changes from 4 commits
6bc50d4
bb81e3d
a718058
2e5c81f
0073964
d4b5305
8971595
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,13 @@ public int MaximumFollowRelLink | |
set { base._maximumFollowRelLink = value; } | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the ResponseHeadersVariable property. | ||
/// </summary> | ||
[Parameter] | ||
[Alias("HV")] | ||
public string ResponseHeadersVariable { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add any validation attributes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the guideline is to still use singular There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I chose If the resulting variable provided a string with the raw Response Header, I would agree. But I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine with it |
||
|
||
#endregion Parameters | ||
|
||
#region Helper Methods | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,6 +95,12 @@ internal override void ProcessResponse(HttpResponseMessage response) | |
{ | ||
StreamHelper.SaveStreamToFile(responseStream, QualifiedOutFile, this); | ||
} | ||
|
||
if (!String.IsNullOrEmpty(ResponseHeadersVariable)) | ||
{ | ||
PSVariableIntrinsics vi = SessionState.PSVariable; | ||
vi.Set(ResponseHeadersVariable, WebResponseHelper.GetHeadersDictionary(response)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we return ReadOnlyDictionary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have seen cases where users are taking the returned dictionary, updating it, and then supplying back to further calls. I don't see much benefit. Also, $Res = invoke-webrequest https://google.com
$Res.BaseResponse.Headers.TryAddWithoutValidation('testy','lala')
$Res.BaseResponse.Headers.GetValues('testy') results with As for the returning entire response, that would be somewhat pointless with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree that we should keep complex self-parsing usage to Invoke-WebRequest and target most common easy to use scenarios for Invoke-RestMethod. This seems fine to me. |
||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2071,6 +2071,28 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { | |
|
||
#endregion charset encoding tests | ||
|
||
Context 'Invoke-RestMethod ResponseHeadersVariable Tests' { | ||
It "Verifies Invoke-RestMethod supports -ResponseHeadersVariable" { | ||
$uri = Get-WebListenerUrl -Test '/' | ||
$response = Invoke-RestMethod -Uri $uri -ResponseHeadersVariable 'headers' | ||
|
||
$headers | Should Not BeNullOrEmpty | ||
$headers.'Content-Type' | Should Be 'text/html; charset=utf-8' | ||
$headers.Server | Should Be 'Kestrel' | ||
} | ||
|
||
It "Verifies Invoke-RestMethod supports -ResponseHeadersVariable overwriting existing variable" { | ||
$uri = Get-WebListenerUrl -Test '/' | ||
$headers = 'prexisting' | ||
$response = Invoke-RestMethod -Uri $uri -ResponseHeadersVariable 'headers' | ||
|
||
$headers | Should Not Be 'prexisting' | ||
$headers | Should Not BeNullOrEmpty | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this test isn't needed as the next line would fail There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. and removed from the other test as well. |
||
$headers.'Content-Type' | Should Be 'text/html; charset=utf-8' | ||
$headers.Server | Should Be 'Kestrel' | ||
} | ||
} | ||
|
||
BeforeEach { | ||
if ($env:http_proxy) { | ||
$savedHttpProxy = $env:http_proxy | ||
|
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.
Maybe 'RHV'?
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'm up for suggestions on this one. I was following suit with
SessionVariable
'sSV
. I would have used-HeadersVariable
for the name if that wouldn't have been mistaken for-Headers
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 prefer RHV too.
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.
updated