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 dfda73811e..de26f49c75 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
@@ -1528,7 +1528,7 @@ namespace Microsoft.PowerShell.Commands
byte[] bytes = null;
XmlDocument doc = xmlNode as XmlDocument;
- if (doc?.FirstChild is XmlDeclaration decl)
+ if (doc?.FirstChild is XmlDeclaration decl && !string.IsNullOrEmpty(decl.Encoding))
{
Encoding encoding = Encoding.GetEncoding(decl.Encoding);
bytes = StreamHelper.EncodeToBytes(doc.OuterXml, encoding);
diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
index 08183e714c..2109fc1fed 100644
--- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
+++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
@@ -588,6 +588,15 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$object.Data | Should -BeExactly 'проверка'
}
+ It "Invoke-WebRequest supports sending XML requests without encoding" {
+ $uri = Get-WebListenerUrl -Test POST
+ $body = ''
+ $result = Invoke-WebRequest -Uri $uri -body ([xml]$body) -ContentType 'text/xml' -method 'POST'
+
+ $object = $result.Content | ConvertFrom-Json
+ $object.Data | Should -BeExactly $body
+ }
+
It "Invoke-WebRequest supports request that returns page containing CodPage 936 data." {
$uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'CP936'
$command = "Invoke-WebRequest -Uri '$uri'"
@@ -2402,6 +2411,14 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$Result.Output.Data | Should -BeExactly 'проверка'
}
+ It "Invoke-RestMethod supports sending XML requests without encoding" {
+ $uri = Get-WebListenerUrl -Test POST
+ $body = ''
+ $result = Invoke-RestMethod -Uri $uri -body ([xml]$body) -ContentType 'text/xml' -method 'POST'
+
+ $result.Data | Should -BeExactly $body
+ }
+
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'"