diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index 90abdf78bba..e9a73b46970 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -60,6 +60,13 @@ public int MaximumFollowRelLink set { base._maximumFollowRelLink = value; } } + /// + /// Gets or sets the ResponseHeadersVariable property. + /// + [Parameter] + [Alias("RHV")] + public string ResponseHeadersVariable { get; set; } + #endregion Parameters #region Helper Methods diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeRestMethodCommand.CoreClr.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeRestMethodCommand.CoreClr.cs index 23c9c0a44f7..fbe452d6d0a 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeRestMethodCommand.CoreClr.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeRestMethodCommand.CoreClr.cs @@ -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)); + } } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 8851bc8657f..206cf637bc8 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -2071,6 +2071,26 @@ 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.'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.'Content-Type' | Should Be 'text/html; charset=utf-8' + $headers.Server | Should Be 'Kestrel' + } + } + BeforeEach { if ($env:http_proxy) { $savedHttpProxy = $env:http_proxy