'Invoke-RestMethod utf8 characters?

I´m doing a PUT from Invoke-RestMethod and the endpoint doesn't like my swedish charcters åäö. I have verified from downloaded curl on my Windows CMD window, and got the same results error 400. The strange thing is that it works from PostMan on my machine. Could it have something to do with utf8 and BOM? tried tried to remove it from my string but it didn´t helped.

Not working:

{    "User":  {
                 "email":  "[email protected]",
                 "last_name":  "Åkesson",
                 "first_name":  "Thomas",
                 "enabled":  true
             }
}

Working:

{
    "User":  {
                 "email":  "[email protected]",
                 "last_name":  "akesson",
                 "first_name":  "Thomas",
                 "enabled":  true
             }
}

Issue seems for me related to Changing PowerShell's default output encoding to UTF-8? Or could the endpoint itself have some bugs?



Solution 1:[1]

Solved it with this line:

$body = [System.Text.Encoding]::UTF8.GetBytes($json)

Answer: https://social.technet.microsoft.com/Forums/ie/en-US/d795e7d2-dcf1-4323-8e06-8f06ce31a897/bug-invokerestmethod-and-utf8-data?forum=winserverpowershell

Solution 2:[2]

For me, this method worked (scenario is pulling from WordPress). See below on Get-Content 'file path' -Encoding UTF8. Maybe Outputting to XML first is the key here ?

#GET WordPress posts.
Invoke-RestMethod -Uri 'https://wordpress.com/category/feed/' -OutFile C:\PowerShell\MakeFeed.xml
#NOTE -Encoding UTF8 is crucial here / otherwise bad characters for superscript, quotes, etc.
[xml]$Content = Get-Content C:\PowerShell\MakeFeed.xml -Encoding UTF8

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user3519275
Solution 2 Geoff Gill