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

Code before:
                    }
                    // Default to try xml first since it's more common
                    else
                    {
                        convertSuccess = TryConvertToXml(str, out obj, ref ex) || TryConvertToJson(str, out obj, ref ex);
                    }

                    if (!convertSuccess)
                    {
                        // Fallback to string
                        obj = str;
                    }

                    WriteObject(obj);
                }
            }
            
            if (ShouldSaveToOutFile)
            {
                string outFilePath = WebResponseHelper.GetOutFilePath(response, _qualifiedOutFile);
                Stream saveStream;

                // Saves the response stream to a file. If piped then the response stream needs to be rewinded.
                if (responseStream != null)
                {
                    responseStream.Position = 0;
                    saveStream = responseStream;
                } 
                else
                {
                    saveStream = baseResponseStream;
                }

                WriteVerbose($"File Name: {Path.GetFileName(outFilePath)}");

                StreamHelper.SaveStreamToFile(saveStream, outFilePath, this, response.Content.Headers.ContentLength.GetValueOrDefault(), perReadTimeout, _cancelToken.Token);
            }

            if (!string.IsNullOrEmpty(StatusCodeVariable))
            {
                PSVariableIntrinsics vi = SessionState.PSVariable;
                vi.Set(StatusCodeVariable, (int)response.StatusCode);
            }

            if (!string.IsNullOrEmpty(ResponseHeadersVariable))
            {
                PSVariableIntrinsics vi = SessionState.PSVariable;
                vi.Set(ResponseHeadersVariable, WebResponseHelper.GetHeadersDictionary(response));
            }
        }


Review comment (refactor):
I suggest to use the same code pattern we used for Invoke-WebRequest
https://github.com/PowerShell/PowerShell/blob/0f13f452e346211abee02e5dace5665c97a5af46/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeWebRequestCommand.CoreClr.cs#L61-L72

Code after:
                    else
                    {
                        convertSuccess = TryConvertToXml(str, out obj, ref ex) || TryConvertToJson(str, out obj, ref ex);
                    }

                    if (!convertSuccess)
                    {
                        // Fallback to string
                        obj = str;
                    }

                    WriteObject(obj);
                }

                responseStream.Position = 0;
            }
            
            if (ShouldSaveToOutFile)
            {
                string outFilePath = WebResponseHelper.GetOutFilePath(response, _qualifiedOutFile);

                WriteVerbose($"File Name: {Path.GetFileName(outFilePath)}");

                StreamHelper.SaveStreamToFile(responseStream, outFilePath, this, response.Content.Headers.ContentLength.GetValueOrDefault(), perReadTimeout, _cancelToken.Token);
            }

            if (!string.IsNullOrEmpty(StatusCodeVariable))
            {
                PSVariableIntrinsics vi = SessionState.PSVariable;
                vi.Set(StatusCodeVariable, (int)response.StatusCode);
            }

            if (!string.IsNullOrEmpty(ResponseHeadersVariable))
            {
                PSVariableIntrinsics vi = SessionState.PSVariable;
                vi.Set(ResponseHeadersVariable, WebResponseHelper.GetHeadersDictionary(response));
            }
        }

        #endregion Virtual Method Overrides

        #region Helper Methods

        private static RestReturnType CheckReturnType(HttpResponseMessage response)
        {
            ArgumentNullException.ThrowIfNull(response);

            RestReturnType rt = RestReturnType.Detect;
            string? contentType = ContentHelper.GetContentType(response);

            if (ContentHelper.IsJson(contentType))