8000 [feature] Make UTF-8 Default Encoding for application/json (#6109) · PowerShell/PowerShell@c6cd66b · GitHub
[go: up one dir, main page]

Skip to content

Commit c6cd66b

Browse files
markekrausiSazonov
authored andcommitted
[feature] Make UTF-8 Default Encoding for application/json (#6109)
When a charset is not supplied for a JSON response, the default encoding should be UTF-8 per RFC 8259. This commit changes the default charset to UTF-8 for JSON responses when a charset is not defined.
1 parent f906e9d commit c6cd66b

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ protected void InitializeContent()
165165
Encoding encoding = null;
166166
// fill the Content buffer
167167
string characterSet = WebResponseHelper.GetCharacterSet(BaseResponse);
168+
169+
if (String.IsNullOrEmpty(characterSet) && ContentHelper.IsJson(contentType))
170+
{
171+
characterSet = Encoding.UTF8.HeaderName;
172+
}
173+
168174
this.Content = StreamHelper.DecodeStream(RawContentStream, characterSet, out encoding);
169175
this.Encoding = encoding;
170176
}

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,11 @@ internal override void ProcessResponse(HttpResponseMessage response)
392392
StreamHelper.TryGetEncoding(charSet, out encoding);
393393
}
394394

395+
if (string.IsNullOrEmpty(charSet) && returnType == RestReturnType.Json)
396+
{
397+
encoding = Encoding.UTF8;
398+
}
399+
395400
object obj = null;
396401
Exception ex = null;
397402

test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,22 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
992992
$response.Output.Encoding.EncodingName | Should Be $expectedEncoding.EncodingName
993993
$response.Output | Should BeOfType 'Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject'
994994
}
995+
996+
It "Verifies Invoke-WebRequest defaults to UTF8 on application/json when no charset is present" {
997+
# when contenttype is set, WebListener suppresses charset unless it is included in the query
998+
$query = @{
999+
contenttype = 'application/json'
1000+
body = '{"Test": "Test"}'
1001+
}
1002+
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
1003+
$expectedEncoding = [System.Text.Encoding]::UTF8
1004+
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing
1005+
1006+
$response.Error | Should BeNullOrEmpty
1007+
$response.Output.Encoding.EncodingName | Should Be $expectedEncoding.EncodingName
1008+
$response.Output | Should BeOfType 'Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject'
1009+
$response.Output.Content | Should BeExactly $query.body
1010+
}
9951011
}
9961012

9971013
#endregion charset encoding tests
@@ -2302,6 +2318,21 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
23022318
$response.Error | Should BeNullOrEmpty
23032319
$response.Encoding.EncodingName | Should Be $expectedEncoding.EncodingName
23042320
}
2321+
2322+
It "Verifies Invoke-RestMethod defaults to UTF8 on application/json when no charset is present" {
2323+
# when contenttype is set, WebListener suppresses charset unless it is included in the query
2324+
$query = @{
2325+
contenttype = 'application/json'
2326+
body = '{"Test": "Test"}'
2327+
}
2328+
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2329+
$expectedEncoding = [System.Text.Encoding]::UTF8
2330+
$response = ExecuteRestMethod -Uri $uri -UseBasicParsing
2331+
2332+
$response.Error | Should BeNullOrEmpty
2333+
$response.Encoding.EncodingName | Should Be $expectedEncoding.EncodingName
2334+
$response.output.Test | Should BeExactly 'Test'
2335+
}
23052336
}
23062337

23072338
#endregion charset encoding tests

0 commit comments

Comments
 (0)
0