Skip to content

Pode fails to convert JSON with BOM + simple fix #1604

@nightroman

Description

@nightroman

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:

Pode/src/Private/Helpers.ps1

Lines 1567 to 1574 in f940490

{ $_ -ilike '*/json' } {
if (Test-PodeIsPSCore) {
$Result.Data = ($Content | ConvertFrom-Json -AsHashtable)
}
else {
$Result.Data = ($Content | ConvertFrom-Json)
}
}

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions