diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index a5a4060e7fd..30c55f506f0 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1277,9 +1277,15 @@ internal virtual void FillRequestStream(HttpRequestMessage request) } } - // Add the content headers - if (request.Content == null) + // For other methods like Put where empty content has meaning, we need to fill in the content + if (request.Content is null) { + // If this is a Get request and there is no content, then don't fill in the content as empty content gets rejected by some web services per RFC7230 + if ((IsStandardMethodSet() && request.Method == HttpMethod.Get && ContentType is null) || (IsCustomMethodSet() && CustomMethod.ToUpperInvariant() == "GET")) + { + return; + } + request.Content = new StringContent(string.Empty); request.Content.Headers.Clear(); } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 0f1f2ca595d..0742578fa83 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -370,6 +370,8 @@ $redirectTests = @( Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { BeforeAll { + $oldProgress = $ProgressPreference + $ProgressPreference = 'SilentlyContinue' $WebListener = Start-WebListener $NotFoundQuery = @{ statuscode = 404 @@ -380,6 +382,10 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { } } + AfterAll { + $ProgressPreference = $oldProgress + } + # Validate the output of Invoke-WebRequest # function ValidateResponse { @@ -456,6 +462,8 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { # Validate response ValidateResponse -response $result + + $result.Output.Headers.'Content-Length' | Should -BeNullOrEmpty } It "Validate Invoke-WebRequest -DisableKeepAlive" { @@ -705,6 +713,13 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { ($result.Output.Content | ConvertFrom-Json).args.testparam | Should -Be "testvalue" } + It 'Validate Invoke-WebRequest empty body CustomMethod GET' { + $uri = Get-WebListenerUrl -Test 'Get' + $command = "Invoke-WebRequest -Uri '$uri' -CustomMethod GET" + $result = ExecuteWebCommand -command $command + $result.Output.Headers.'Content-Length' | Should -BeNullOrEmpty + } + It "Validate Invoke-WebRequest body is converted to query params for CustomMethod GET and -NoProxy" { $uri = Get-WebListenerUrl -Test 'Get' $command = "Invoke-WebRequest -Uri '$uri' -CustomMethod GET -Body @{'testparam'='testvalue'} -NoProxy" @@ -2061,6 +2076,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { BeforeAll { + $oldProgress = $ProgressPreference + $ProgressPreference = 'SilentlyContinue' + $WebListener = Start-WebListener $NotFoundQuery = @{ @@ -2072,6 +2090,10 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { } } + AfterAll { + $ProgressPreference = $oldProgress + } + #User-Agent changes on different platforms, so tests should only be run if on the correct platform It "Invoke-RestMethod returns Correct User-Agent on MacOSX" -Skip:(!$IsMacOS) { $uri = Get-WebListenerUrl -Test 'Get' @@ -2111,6 +2133,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { # Validate response $result.Error | Should -BeNullOrEmpty + $result.Output.headers.'Content-Length' | Should -Be 0 } It "Invoke-RestMethod returns headers dictionary" { @@ -2350,6 +2373,13 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $result.Output.args.testparam | Should -Be "testvalue" } + It 'Validate Invoke-RestMethod empty body CustomMethod GET' { + $uri = Get-WebListenerUrl -Test 'Get' + $command = "Invoke-RestMethod -Uri '$uri' -CustomMethod GET" + $result = ExecuteWebCommand -command $command + $result.Output.Headers.'Content-Length' | Should -BeNullOrEmpty + } + It "Validate Invoke-RestMethod body is converted to query params for CustomMethod GET and -NoProxy" { $uri = Get-WebListenerUrl -Test 'Get' $command = "Invoke-RestMethod -Uri '$uri' -CustomMethod GET -Body @{'testparam'='testvalue'} -NoProxy" @@ -3528,9 +3558,15 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { Describe "Validate Invoke-WebRequest and Invoke-RestMethod -InFile" -Tags "Feature", "RequireAdminOnWindows" { BeforeAll { + $oldProgress = $ProgressPreference + $ProgressPreference = 'SilentlyContinue' $WebListener = Start-WebListener } + AfterAll { + $ProgressPreference = $oldProgress + } + Context "InFile parameter negative tests" { BeforeAll { $uri = Get-WebListenerUrl -Test 'Post'