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 95e2791fc..a669a3123 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 @@ -1674,7 +1674,7 @@ namespace Microsoft.PowerShell.Commands // would be used if Charset is not supplied in the Content-Type property. try { - var mediaTypeHeaderValue = new MediaTypeHeaderValue(ContentType); + var mediaTypeHeaderValue = MediaTypeHeaderValue.Parse(ContentType); if (!string.IsNullOrEmpty(mediaTypeHeaderValue.CharSet)) { encoding = Encoding.GetEncoding(mediaTypeHeaderValue.CharSet); diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index da97cc4d8..9f69785cd 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -482,6 +482,19 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $Result.Output.Content | Should -Match '⡌⠁⠧⠑ ⠼⠁⠒ ⡍⠜⠇⠑⠹⠰⠎ ⡣⠕⠌' } + It "Invoke-WebRequest supports sending request as UTF-8." { + $uri = Get-WebListenerUrl -Test 'POST' + # Body must contain non-ASCII characters + $command = "Invoke-WebRequest -Uri '$uri' -Body 'проверка' -ContentType 'application/json; charset=utf-8' -Method 'POST'" + + $result = ExecuteWebCommand -command $command + ValidateResponse -response $result + + $Result.Output.Encoding.BodyName | Should -BeExactly 'utf-8' + $object = $Result.Output.Content | ConvertFrom-Json + $object.Data | Should -BeExactly 'проверка' + } + It "Invoke-WebRequest supports request that returns page containing CodPage 936 data." { $uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'CP936' $command = "Invoke-WebRequest -Uri '$uri'" @@ -1902,6 +1915,15 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $Result.Output | Should -Match '⡌⠁⠧⠑ ⠼⠁⠒ ⡍⠜⠇⠑⠹⠰⠎ ⡣⠕⠌' } + It "Invoke-RestMethod supports sending requests as UTF8" { + $uri = Get-WebListenerUrl -Test POST + # Body must contain non-ASCII characters + $command = "Invoke-RestMethod -Uri '$uri' -body 'проверка' -ContentType 'application/json; charset=utf-8' -method 'POST'" + + $result = ExecuteWebCommand -command $command + $Result.Output.Data | Should -BeExactly 'проверка' + } + It "Invoke-RestMethod supports request that returns page containing Code Page 936 data." { $uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'CP936' $command = "Invoke-RestMethod -Uri '$uri'"