3

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":  "xxxxx@domain.com",
                 "last_name":  "Åkesson",
                 "first_name":  "Thomas",
                 "enabled":  true
             }
}

Working:

{
    "User":  {
                 "email":  "xxxxx@domain.com",
                 "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?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
user3519275
  • 138
  • 2
  • 9

3 Answers3

6

Solved it with this line:

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

Answer: Microsoft: Invoke-RestMethod and UTF-8 data

shA.t
  • 16,580
  • 5
  • 54
  • 111
user3519275
  • 138
  • 2
  • 9
2

I had this issue with danish characters åæø when trying to post something in teams using PowerShell. I found that the solution was to include the character encoding in the call:

$FilePath = '.\body.txt'
$fileContent = Get-Content -Path $FilePath -Encoding UTF8 
Write-host $fileContent
Invoke-RestMethod -ContentType "application/json; charset=windows-1252" -Uri "https://example.webhook.office.com/webhookb2" -Method 'Post' -Body $fileContent

i.e. including charset=windows-1252 did the trick for me.

shA.t
  • 16,580
  • 5
  • 54
  • 111
NDM
  • 509
  • 5
  • 4
0

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