Fix Request Charset Issues in Web Cmdlets (#8742)
Instantiating a new MediaTypeHeaderValue object fails when the -ContentType parameter includes a charset such as application/json; charset=utf-8. This makes it impossible to set the content encoding on web requests. Moving to Parse() ensures we actually get a proper MediaTypeHeaderValue when the charset is present, thus allowing users to set their request encoding via proper -ContentType values.
This commit is contained in:
parent
43b68c43cc
commit
ff83206e1c
|
@ -1674,7 +1674,7 @@ namespace Microsoft.PowerShell.Commands
|
||||||
// would be used if Charset is not supplied in the Content-Type property.
|
// would be used if Charset is not supplied in the Content-Type property.
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var mediaTypeHeaderValue = new MediaTypeHeaderValue(ContentType);
|
var mediaTypeHeaderValue = MediaTypeHeaderValue.Parse(ContentType);
|
||||||
if (!string.IsNullOrEmpty(mediaTypeHeaderValue.CharSet))
|
if (!string.IsNullOrEmpty(mediaTypeHeaderValue.CharSet))
|
||||||
{
|
{
|
||||||
encoding = Encoding.GetEncoding(mediaTypeHeaderValue.CharSet);
|
encoding = Encoding.GetEncoding(mediaTypeHeaderValue.CharSet);
|
||||||
|
|
|
@ -482,6 +482,19 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
|
||||||
$Result.Output.Content | Should -Match '⡌⠁⠧⠑ ⠼⠁⠒ ⡍⠜⠇⠑⠹⠰⠎ ⡣⠕⠌'
|
$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." {
|
It "Invoke-WebRequest supports request that returns page containing CodPage 936 data." {
|
||||||
$uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'CP936'
|
$uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'CP936'
|
||||||
$command = "Invoke-WebRequest -Uri '$uri'"
|
$command = "Invoke-WebRequest -Uri '$uri'"
|
||||||
|
@ -1902,6 +1915,15 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
|
||||||
$Result.Output | Should -Match '⡌⠁⠧⠑ ⠼⠁⠒ ⡍⠜⠇⠑⠹⠰⠎ ⡣⠕⠌'
|
$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." {
|
It "Invoke-RestMethod supports request that returns page containing Code Page 936 data." {
|
||||||
$uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'CP936'
|
$uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'CP936'
|
||||||
$command = "Invoke-RestMethod -Uri '$uri'"
|
$command = "Invoke-RestMethod -Uri '$uri'"
|
||||||
|
|
Loading…
Reference in a new issue