REST API : Accented characters issue

Hi gang!

Here’s my endpoint :

New-PSUEndpoint -Url "/accentedchars" -Endpoint {
"Caractères accentués <- Accented characters issue"
}

It’s running under PowerShell 7.

If I consume my endpoint inside of a PowerShell 7 console it works fine. But if I run it with Windows PowerShell I’m facing an encoding issue and my accented characters are wrongly encoded.

See below:

PS > Invoke-RestMethod http://localhost:8080/accentedchars
Caractères accentués <- Accented characters issue

I need to use Windows PowerShell 5.1 on the client side, hence I need to find a solution.

Thanks for your help.

Product: PowerShell Universal
Version: 1.5.15

While not ideal, I found a stack overflow post with this solution for winps.

[system.Text.Encoding]::UTF8.GetString((Invoke-WebRequest http://localhost:5000/accent).RawContentStream.ToArray())

I did find this open PS issue but it seems to work fine on 7 like you said: Invoke-RestMethod/Invoke-WebRequest should set charset=utf-8 for application/json by default · Issue #10971 · PowerShell/PowerShell · GitHub

Using the -ContentType work around doesn’t work for me

Thanks Adam! This is very helpful!

1 Like