Pode icon indicating copy to clipboard operation
Pode copied to clipboard

Pode fails to convert JSON with BOM + simple fix

Open nightroman opened this issue 3 months ago • 4 comments

VSCode REST Client extension may send JSON file content with BOM. This results in Pode exceptions on converting such JSON.

Steps to reproduce

Use the attached files, start server.ps1 (port 9999).

2025-08-30-0729-Pode-JSON-BOM.zip

Example with BOM and problem

Invoke "REST Client: Send Request" from BOM-yes.json.http which includes BOM-yes.json with BOM. This results in this Pode exception:

Conversion from JSON failed with error: Unexpected character encountered while parsing value: . Path '', line 0, position 0.
At C:\Program Files\PowerShell\Modules\Pode\2.12.1\Private\Helpers.ps1:1569 char:44
+ …             $Result.Data = ($Content | ConvertFrom-Json -AsHashtable)
+                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
at ConvertFrom-PodeRequestContent, C:\Program Files\PowerShell\Modules\Pode\2.12.1\Private\Helpers.ps1: line 1569
at <ScriptBlock>, C:\Program Files\PowerShell\Modules\Pode\2.12.1\Private\Middleware.ps1: line 312
at Invoke-PodeScriptBlock, C:\Program Files\PowerShell\Modules\Pode\2.12.1\Public\Utilities.ps1: line 615
at Invoke-PodeMiddleware, C:\Program Files\PowerShell\Modules\Pode\2.12.1\Private\Middleware.ps1: line 41
at <ScriptBlock>, <No file>: line 97
    + NotSpecified: (:) [ConvertFrom-Json], ArgumentException

Presumably JSON with BOM is a problem for ConvertFrom-Json in this Private/Helpers.ps1 code: https://github.com/Badgerati/Pode/blob/f940490890fc5a8d0652e245504d0b3a6f8451ee/src/Private/Helpers.ps1#L1567-L1574

Example with no BOM and no problem

Just to show that the approach works as such.

Invoke "REST Client: Send Request" from BOM-no.json.http which includes BOM-no.json with no BOM. This works, REST Client shows the response data.

Simple fix

Adding the remove BOM block before calling $Content | ConvertFrom-Json solves the problem:

        { $_ -ilike '*/json' } {
            # remove BOM
            if ($Content.StartsWith([char]0xFEFF)) {
                $Content = $Content.Substring(1)
            }

            if (Test-PodeIsPSCore) {
                $Result.Data = ($Content | ConvertFrom-Json -AsHashtable)
            }
            else {
                $Result.Data = ($Content | ConvertFrom-Json)
            }
        }

If this looks reasonable, please apply the suggested fix. Thank you in advance.

Versions

  • Pode v2.12.1
  • REST Client 0.25.1
  • VSCode latest official

nightroman avatar Aug 30 '25 07:08 nightroman

On the second thought, maybe it's not a fix but work around the issue that BOM gets into $Content.

nightroman avatar Sep 01 '25 19:09 nightroman

Maybe this line https://github.com/Badgerati/Pode/blob/f940490890fc5a8d0652e245504d0b3a6f8451ee/src/Private/Helpers.ps1#L2928

should include the encoding parameter:

$body = [System.IO.StreamReader]::new($stream, [System.Text.Encoding]::UTF8).ReadToEnd()

NB Side question: should not we .Close() this created stream reader after reading?

nightroman avatar Sep 01 '25 20:09 nightroman

Maybe not... help about the constructor without encoding says

This constructor initializes the encoding to UTF8Encoding

nightroman avatar Sep 01 '25 20:09 nightroman

Hi @nightroman,

From what I've read, it might appear that your original suggestion is actually the best - though we might be able to move the BOM removal further up the stack, and remove it during initial request parsing in the .NET listener. 🤔

Badgerati avatar Sep 01 '25 22:09 Badgerati