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 567a98ebfb9..9860d8c2179 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 @@ -1488,14 +1488,16 @@ internal void SetRequestContent(HttpRequestMessage request, string content) ArgumentNullException.ThrowIfNull(content); Encoding encoding = null; - if (ContentType is not null) + string contentType = WebSession.ContentHeaders[HttpKnownHeaderNames.ContentType]; + + if (contentType is not null) { // If Content-Type contains the encoding format (as CharSet), use this encoding format // to encode the Body of the WebRequest sent to the server. Default Encoding format // would be used if Charset is not supplied in the Content-Type property. try { - MediaTypeHeaderValue mediaTypeHeaderValue = MediaTypeHeaderValue.Parse(ContentType); + MediaTypeHeaderValue mediaTypeHeaderValue = MediaTypeHeaderValue.Parse(contentType); if (!string.IsNullOrEmpty(mediaTypeHeaderValue.CharSet)) { encoding = Encoding.GetEncoding(mediaTypeHeaderValue.CharSet); @@ -1506,7 +1508,7 @@ internal void SetRequestContent(HttpRequestMessage request, string content) if (!SkipHeaderValidation) { ValidationMetadataException outerEx = new(WebCmdletStrings.ContentTypeException, ex); - ErrorRecord er = new(outerEx, "WebCmdletContentTypeException", ErrorCategory.InvalidArgument, ContentType); + ErrorRecord er = new(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 2da345fcd3a..01946e5c5f0 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -568,6 +568,13 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $Result.Output.Content | Should -Match '⡌⠁⠧⠑ ⠼⠁⠒ ⡍⠜⠇⠑⠹⠰⠎ ⡣⠕⠌' } + It "Invoke-WebRequest -ContentType overwrites Content-Type from -Headers." { + $uri = Get-WebListenerUrl -Test 'POST' + $command = "Invoke-WebRequest -Uri '$uri' -ContentType 'application/json' -Headers @{'Content-Type'='plain/text'} -Method 'POST'" + $result = ExecuteWebCommand -command $command + $result.Output.BaseResponse.RequestMessage.Content.Headers.ContentType.MediaType | Should -BeExactly 'application/json' + } + It "Invoke-WebRequest supports sending request as UTF-8." { $uri = Get-WebListenerUrl -Test 'POST' # Body must contain non-ASCII characters @@ -576,7 +583,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $result = ExecuteWebCommand -command $command ValidateResponse -response $result - $Result.Output.Encoding.BodyName | Should -BeExactly 'utf-8' + $result.Output.Encoding.BodyName | Should -BeExactly 'utf-8' $object = $Result.Output.Content | ConvertFrom-Json $object.Data | Should -BeExactly 'проверка' } @@ -2338,6 +2345,13 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $Result.Output | Should -Match '⡌⠁⠧⠑ ⠼⠁⠒ ⡍⠜⠇⠑⠹⠰⠎ ⡣⠕⠌' } + It "Invoke-RestMethod -ContentType overwrites Content-Type from -Headers." { + $uri = Get-WebListenerUrl -Test 'POST' + $command = "Invoke-RestMethod -Uri '$uri' -ContentType 'application/json' -Headers @{'Content-Type'='plain/text'} -Method 'POST'" + $result = ExecuteWebCommand -command $command + $result.Output.headers."Content-Type" | Should -BeExactly 'application/json' + } + It "Invoke-RestMethod supports sending requests as UTF8" { $uri = Get-WebListenerUrl -Test POST # Body must contain non-ASCII characters