Skip to content

Fix WebResponseObject ToString encoding#27178

Open
badgerbees wants to merge 1 commit intoPowerShell:masterfrom
badgerbees:fix/webresponseobject-tostring-utf8
Open

Fix WebResponseObject ToString encoding#27178
badgerbees wants to merge 1 commit intoPowerShell:masterfrom
badgerbees:fix/webresponseobject-tostring-utf8

Conversation

@badgerbees
Copy link
Copy Markdown

@badgerbees badgerbees commented Apr 2, 2026

PR Summary

Fix WebResponseObject.ToString() so non-ASCII response content is preserved by decoding with the response charset instead of ASCII. Keep the legacy RawContent preview behavior unchanged and add a regression test for a UTF-8 response body, also this fixes issue #27154.

PR Context

Invoke-WebRequest responses were losing non-ASCII characters when callers relied on .ToString(). This change keeps the fix narrow by updating the stringification path only, leaving the existing RawContent preview logic alone and covering the reported UTF-8 case with a regression test.

PR Checklist

Copilot AI review requested due to automatic review settings April 2, 2026 23:44
@badgerbees badgerbees requested a review from a team as a code owner April 2, 2026 23:44
@badgerbees
Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes WebResponseObject.ToString() so response bodies preserve non-ASCII characters by decoding using the response charset (falling back to the WebCmdlet default encoding), while keeping the legacy ASCII-based RawContent preview behavior unchanged.

Changes:

  • Update WebResponseObject.ToString() to decode Content using the response Content-Type charset via StreamHelper.TryGetEncoding(...) instead of ASCII.
  • Preserve the existing ASCII RawContent preview output by moving the old preview logic into a dedicated helper.
  • Add a regression assertion ensuring .ToString() preserves UTF-8 response content in Invoke-WebRequest tests.

Reviewed changes

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

File Description
src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebResponseObject.Common.cs Decodes .ToString() using the response charset while preserving the ASCII RawContent preview behavior via a new helper.
test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 Adds a regression check that .ToString() contains expected UTF-8 characters.

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.

2 participants