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 ddac58fe57d..4c5d3e82275 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 @@ -1260,21 +1260,24 @@ internal virtual void FillRequestStream(HttpRequestMessage request) foreach (var entry in WebSession.ContentHeaders) { - if (SkipHeaderValidation) - { - request.Content.Headers.TryAddWithoutValidation(entry.Key, entry.Value); - } - else + if (!string.IsNullOrWhiteSpace(entry.Value)) { - try + if (SkipHeaderValidation) { - request.Content.Headers.Add(entry.Key, entry.Value); + request.Content.Headers.TryAddWithoutValidation(entry.Key, entry.Value); } - catch (FormatException ex) + else { - var outerEx = new ValidationMetadataException(WebCmdletStrings.ContentTypeException, ex); - ErrorRecord er = new ErrorRecord(outerEx, "WebCmdletContentTypeException", ErrorCategory.InvalidArgument, ContentType); - ThrowTerminatingError(er); + try + { + request.Content.Headers.Add(entry.Key, entry.Value); + } + catch (FormatException ex) + { + var outerEx = new ValidationMetadataException(WebCmdletStrings.ContentTypeException, ex); + ErrorRecord er = new ErrorRecord(outerEx, "WebCmdletContentTypeException", ErrorCategory.InvalidArgument, ContentType); + ThrowTerminatingError(er); + } } } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index d4ab2ff5009..2e234ea5940 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -447,6 +447,16 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $jsonContent.headers.Host | Should -Be $Uri.Authority } + It "Invoke-WebRequest with blank ContentType succeeds" { + $uri = Get-WebListenerUrl -Test 'Get' + $command = "Invoke-WebRequest -Uri '$uri' -ContentType ''" + + $result = ExecuteWebCommand -command $command + + # Validate response + ValidateResponse -response $result + } + It "Validate Invoke-WebRequest -DisableKeepAlive" { # Operation options $uri = Get-WebListenerUrl -Test 'Get' @@ -1995,6 +2005,16 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $result.Output.headers.'User-Agent' | Should -MatchExactly '.*\(Windows NT \d+\.\d*;.*\) PowerShell\/\d+\.\d+\.\d+.*' } + It "Invoke-RestMethod with blank ContentType succeeds" { + $uri = Get-WebListenerUrl -Test 'Get' + $command = "Invoke-RestMethod -Uri '$uri' -ContentType ''" + + $result = ExecuteWebCommand -command $command + + # Validate response + $result.Error | Should -BeNullOrEmpty + } + It "Invoke-RestMethod returns headers dictionary" { $uri = Get-WebListenerUrl -Test 'Get' $command = "Invoke-RestMethod -Uri '$uri'"