Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix(web-cmdlets): use response charset instead of ASCII in WebResponseObject.ToString() - #27742

#27742
Open
sergioperezcheco wants to merge 1 commit into
PowerShell:masterPowerShell/PowerShell:masterfrom
sergioperezcheco:fix/webresponse-tostring-encodingsergioperezcheco/PowerShell:fix/webresponse-tostring-encodingCopy head branch name to clipboard
Open

fix(web-cmdlets): use response charset instead of ASCII in WebResponseObject.ToString()#27742
sergioperezcheco wants to merge 1 commit into
PowerShell:masterPowerShell/PowerShell:masterfrom
sergioperezcheco:fix/webresponse-tostring-encodingsergioperezcheco/PowerShell:fix/webresponse-tostring-encodingCopy head branch name to clipboard

Conversation

@sergioperezcheco

Copy link
Copy Markdown

WebResponseObject.ToString() hardcoded Encoding.ASCII to decode the response body bytes, so any non-ASCII character came out as '?' (or '.' after the printable-char filter). This affects both the direct .ToString() call and the RawContent property that is built from it.

This change resolves the encoding from the response Content-Type charset via WebResponseHelper.GetCharacterSet / StreamHelper.TryGetEncoding, falling back to UTF-8 (ContentHelper.GetDefaultEncoding) when no charset is declared. UTF-8 is a strict superset of ASCII, so existing pure-ASCII responses are unchanged. I also updated the now-stale 'Use ASCII encoding' comment in InitializeRawContent.

Added a Pester test that serves a UTF-8 body containing 'café' and asserts the non-ASCII character survives ToString().

Resolves #27154.

…eObject.ToString()

ToString() hardcoded Encoding.ASCII to decode the response body bytes,
which silently replaced any non-ASCII character with '?' (or '.'). This
affected both the user-facing .ToString() call and the RawContent property
that feeds into it.

Use the character set declared in the response Content-Type header, falling
back to UTF-8 (the default used by the rest of the web cmdlets) when no
charset is present. UTF-8 is a strict superset of ASCII, so pure-ASCII
responses are unaffected.

Resolves PowerShell#27154.

Signed-off-by: sergioperezcheco <checo520@outlook.com>
@sergioperezcheco
sergioperezcheco requested a review from a team as a code owner July 28, 2026 12:39
Copilot AI review requested due to automatic review settings July 28, 2026 12:39
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes WebResponseObject.ToString() decoding so response bodies with non-ASCII characters are preserved by using the response Content-Type charset (falling back to UTF-8), addressing issue #27154 and improving RawContent fidelity.

Changes:

  • Update WebResponseObject.ToString() to resolve the encoding from the response charset via WebResponseHelper.GetCharacterSet + StreamHelper.TryGetEncoding (defaulting to UTF-8).
  • Update the InitializeRawContent comment to reflect the new encoding behavior.
  • Add a Pester test validating non-ASCII preservation for a UTF-8 response body.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebResponseObject.Common.cs Switches ToString() decoding from hardcoded ASCII to response charset/default UTF-8 and updates a related comment.
test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 Adds a regression test ensuring Invoke-WebRequest preserves non-ASCII characters through .ToString().

Comment on lines +187 to +193
// Decode using the response's character set when available so non-ASCII
// content is preserved. Fall back to UTF-8, which is a safe superset of
// ASCII and the default encoding used elsewhere in the web cmdlets.
string? characterSet = WebResponseHelper.GetCharacterSet(BaseResponse);
StreamHelper.TryGetEncoding(characterSet, out Encoding encoding);

char[] stringContent = encoding.GetChars(Content);
Comment on lines +1537 to +1549
It "Verifies Invoke-WebRequest ToString preserves non-ASCII characters when charset is declared" {
$query = @{
contenttype = 'text/plain; charset=utf-8'
body = 'café'
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing

$response.Error | Should -BeNullOrEmpty
# ToString() should preserve the non-ASCII 'é' instead of replacing it with '?'.
$response.Output.ToString() | Should -Match 'café'
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invoke-WebRequest's .ToString() assumes content is ASCII

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.