Skip to content

Commit

Permalink
Fix Request Charset Issues in Web Cmdlets (PowerShell#8742)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
markekraus authored and iSazonov committed Jan 26, 2019
1 parent 43b68c4 commit ff83206
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,7 @@ internal long SetRequestContent(HttpRequestMessage request, string content)
// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
Expand Down Expand Up @@ -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'"
Expand Down

0 comments on commit ff83206

Please sign in to comment.