API Post with swedish characters in body

  • Product: PowerShell Universal
  • Version: 2.8.0

I have an API Endpoint where I wan’t to post swedish characters like åäö.
The problem is that the endpoint interprets the characters wrong.

New-PSUEndpoint -Url “/BodyTest” -Method “POST” -Endpoint {
Param ( $Text )
Add-Content C:\Temp\PSUlog.txt “Text = $Text”
“PSVersion” = $($PSVersionTable.PSVersion.ToString())
“Body” = $Text
} -ErrorAction “Stop” -Timeout 0

$Body = @{
“Text” = “Testing text with swedish characters - åäö”
} | ConvertTo-Json
Invoke-RestMethod -Uri “http://localhost:5000/BodyTest” -Body $Body -Method “Post” -ContentType “application/json”

PSVersion Body

7.1.5 Testing text with swedish characters - ���

The logfile that is written locally says
Text = Testing text with swedish characters - ���

I think I found a solution. This one did the trick
$Body = [System.Text.Encoding]::UTF8.GetBytes($Body)

PS 5.1 Client
The logfile output shows up correct but the output in the terminal still shows wrong type of characters.

PS 72.1
The output and the logfile show the correct characters

Hello I don’t know if it helps, but I always call the api with the parameters -ContentType ‘application/json;charset=utf-8’ to prevent french special chars to be changed by wrong chars in the result.