Skip to content

Commit 3237d43

Browse files
markekrausadityapatwardhan
authored andcommitted
Add ResponseHeadersVariable Parameter to Invoke-RestMethod (PowerShell#4888)
* Add ResponseHeadersVariable to Invoke-RestMethod * Add Tests for -ResponseHeadersVariable * [Feature] Run Feature Tests * Remove test to address PR feedback * Alais HV -> RHV per request * [Feature] Rerun CI * [Feature] Remove Superfluous Assertion
1 parent 1c921cd commit 3237d43

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ public int MaximumFollowRelLink
6060
set { base._maximumFollowRelLink = value; }
6161
}
6262

63+
/// <summary>
64+
/// Gets or sets the ResponseHeadersVariable property.
65+
/// </summary>
66+
[Parameter]
67+
[Alias("RHV")]
68+
public string ResponseHeadersVariable { get; set; }
69+
6370
#endregion Parameters
6471

6572
#region Helper Methods

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeRestMethodCommand.CoreClr.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ internal override void ProcessResponse(HttpResponseMessage response)
9595
{
9696
StreamHelper.SaveStreamToFile(responseStream, QualifiedOutFile, this);
9797
}
98+
99+
if (!String.IsNullOrEmpty(ResponseHeadersVariable))
100+
{
101+
PSVariableIntrinsics vi = SessionState.PSVariable;
102+
vi.Set(ResponseHeadersVariable, WebResponseHelper.GetHeadersDictionary(response));
103+
}
98104
}
99105
}
100106

test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,6 +2055,26 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
20552055

20562056
#endregion charset encoding tests
20572057

2058+
Context 'Invoke-RestMethod ResponseHeadersVariable Tests' {
2059+
It "Verifies Invoke-RestMethod supports -ResponseHeadersVariable" {
2060+
$uri = Get-WebListenerUrl -Test '/'
2061+
$response = Invoke-RestMethod -Uri $uri -ResponseHeadersVariable 'headers'
2062+
2063+
$headers.'Content-Type' | Should Be 'text/html; charset=utf-8'
2064+
$headers.Server | Should Be 'Kestrel'
2065+
}
2066+
2067+
It "Verifies Invoke-RestMethod supports -ResponseHeadersVariable overwriting existing variable" {
2068+
$uri = Get-WebListenerUrl -Test '/'
2069+
$headers = 'prexisting'
2070+
$response = Invoke-RestMethod -Uri $uri -ResponseHeadersVariable 'headers'
2071+
2072+
$headers | Should Not Be 'prexisting'
2073+
$headers.'Content-Type' | Should Be 'text/html; charset=utf-8'
2074+
$headers.Server | Should Be 'Kestrel'
2075+
}
2076+
}
2077+
20582078
BeforeEach {
20592079
if ($env:http_proxy) {
20602080
$savedHttpProxy = $env:http_proxy

0 commit comments

Comments
 (0)