Repository: PowerShell/PowerShell
File: src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs (C#)

Code before:

        /// <summary>
        /// Gets or sets the variable name to use for storing the status code from the response.
        /// </summary>
        [Parameter]
        public string? StatusCodeVariable { get; set; }

        #endregion Parameters

        #region Virtual Method Overrides

        /// <summary>
        /// Process the web response and output corresponding objects.
        /// </summary>
        /// <param name="response"></param>
        internal override void ProcessResponse(HttpResponseMessage response)
        {
            ArgumentNullException.ThrowIfNull(response);
            ArgumentNullException.ThrowIfNull(_cancelToken);

            TimeSpan perReadTimeout = ConvertTimeoutSecondsToTimeSpan(OperationTimeoutSeconds);
            Stream baseResponseStream = StreamHelper.GetResponseStream(response, _cancelToken.Token);

            if (ShouldWriteToPipeline)
            {
                using BufferingStreamReader responseStream = new(baseResponseStream, perReadTimeout, _cancelToken.Token);

                // First see if it is an RSS / ATOM feed, in which case we can
                // stream it - unless the user has overridden it with a return type of "XML"
                if (TryProcessFeedStream(responseStream))
                {
                    // Do nothing, content has been processed.
                }
                else
                {
                    // Try to get the response encoding from the ContentType header.
                    string? characterSet = WebResponseHelper.GetCharacterSet(response);
                    string str = StreamHelper.DecodeStream(responseStream, characterSet, out Encoding encoding, perReadTimeout, _cancelToken.Token);

                    string encodingVerboseName;
                    try
                    {
                        encodingVerboseName = encoding.HeaderName;
                    }
                    catch
                    {
                        encodingVerboseName = string.Empty;
                    }

                    // NOTE: Tests use this verbose output to verify the encoding.
                    WriteVerbose(string.Create(System.Globalization.CultureInfo.InvariantCulture, $"Content encoding: {encodingVerboseName}"));

Review comment (refactor):
The code used for `Invoke-WebRequest` assigns `responseStream` back to the original stream object, but your change stops doing that. I'm not sure if that could cause any issue.

https://github.com/PowerShell/PowerShell/blob/0f13f452e346211abee02e5dace5665c97a5af46/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeWebRequestCommand.CoreClr.cs#L58-L62

Can you still keep the `baseStream` and re-position the base stream to position 0 instead? So `if (ShouldSaveToOutFile)` will continue to use the base stream as before.

Code after:

        /// <summary>
        /// Gets or sets the variable name to use for storing the status code from the response.
        /// </summary>
        [Parameter]
        public string? StatusCodeVariable { get; set; }

        #endregion Parameters

        #region Virtual Method Overrides

        /// <summary>
        /// Process the web response and output corresponding objects.
        /// </summary>
        /// <param name="response"></param>
        internal override void ProcessResponse(HttpResponseMessage response)
        {
            ArgumentNullException.ThrowIfNull(response);
            ArgumentNullException.ThrowIfNull(_cancelToken);

            TimeSpan perReadTimeout = ConvertTimeoutSecondsToTimeSpan(OperationTimeoutSeconds);
            Stream responseStream = StreamHelper.GetResponseStream(response, _cancelToken.Token);

            if (ShouldWriteToPipeline)
            {
                responseStream = new BufferingStreamReader(responseStream, perReadTimeout, _cancelToken.Token);

                // First see if it is an RSS / ATOM feed, in which case we can
                // stream it - unless the user has overridden it with a return type of "XML"
                if (TryProcessFeedStream(responseStream))
                {
                    // Do nothing, content has been processed.
                }
                else
                {
                    // Try to get the response encoding from the ContentType header.
                    string? characterSet = WebResponseHelper.GetCharacterSet(response);
                    string str = StreamHelper.DecodeStream(responseStream, characterSet, out Encoding encoding, perReadTimeout, _cancelToken.Token);

                    string encodingVerboseName;
                    try
                    {
                        encodingVerboseName = encoding.HeaderName;
                    }
                    catch
                    {
                        encodingVerboseName = string.Empty;
                    }

                    // NOTE: Tests use this verbose output to verify the encoding.
                    WriteVerbose($"Content encoding: {encodingVerboseName}");