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

Code before:
                }
                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 encodingName = string.Empty;
                    string encodingHeaderName = string.Empty;
                    string encodingPage = encoding.CodePage == -1 ? "unknown" : encoding.CodePage.ToString();
                    try
                    {
                        // NOTE: These are getter methods that may possibly throw a NotSupportedException exception,
                        // hence the try/catch
                        encodingHeaderName = encoding.HeaderName;
                        encodingName = encoding.BodyName;
                    }
                    catch
                    {
                    }

                    // NOTE: Tests use this debug output to verify the encoding.
                    if (encodingHeaderName != string.Empty && encodingName != string.Empty)
                    {
                        WriteDebug($"WebResponse content encoding: {encodingHeaderName} ({encodingName}) CodePage: {encodingPage}");
                    }

                    // Determine the response type
                    RestReturnType returnType = CheckReturnType(response);

                    bool convertSuccess = false;
                    object? obj = null;
                    Exception? ex = null;

                    if (returnType == RestReturnType.Json)
                    {
                        convertSuccess = TryConvertToJson(str, out obj, ref ex) || TryConvertToXml(str, out obj, ref ex);
                    }
                    // 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);

Review comment (suggestion):
I think we should write in any case without the condition. And replace `string.Empty` with "unknown" or "-".

Code after:
                }
                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 friendlyName = "unknown";
                    string encodingWebName = "unknown";
                    string encodingPage = encoding.CodePage == -1 ? "unknown" : encoding.CodePage.ToString();
                    try
                    {
                        // NOTE: These are getter methods that may possibly throw a NotSupportedException exception,
                        // hence the try/catch
                        encodingWebName = encoding.WebName;
                        friendlyName = encoding.EncodingName;
                    }
                    catch
                    {
                    }

                    // NOTE: Tests use this debug output to verify the encoding.
                    WriteDebug($"WebResponse content encoding: {encodingWebName} ({friendlyName}) CodePage: {encodingPage}");

                    // Determine the response type
                    RestReturnType returnType = CheckReturnType(response);

                    bool convertSuccess = false;
                    object? obj = null;
                    Exception? ex = null;

                    if (returnType == RestReturnType.Json)
                    {
                        convertSuccess = TryConvertToJson(str, out obj, ref ex) || TryConvertToXml(str, out obj, ref ex);
                    }
                    // 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);
                }

                responseStream.Position = 0;