user10879058
user10879058

Reputation: 3

How to encode german chars (like ü) with powershell for REST API?

I'm creating users from PowerShell scripts by POSTing data collected from AD to a REST API enabled web portal, using:

$response = Invoke-RestMethod -Method Post -Uri $url -Headers $headers -Body $body

where $body is a JSON formatted propper payload, like this:

{
"email" : "[email protected]",
"firstname" : "Klaus",
"lastname" : "Müller",
"active" : "true",
"superadmin" : "false"
}

If there is no german letter in name, all is O.K. and user is created. When there is a german character, I get 400 : Bad Request response from server.

Can I somehow change the behaviour of Invoke-RestMethod with setting encoding or its the server maybe configured not to receive de characters?

Upvotes: 0

Views: 1080

Answers (1)

Floogen
Floogen

Reputation: 306

This is similar to this question.

As @Keith Hill stated

The string in PowerShell is Unicode but you've specified a UTF8 encoding so I think you need to give it some help getting to UTF8.

While the linked thread refers to Invoke-WebRequest, the encoding should still be valid for Invoke-RestMethod.

Your request should look something like this

$response = Invoke-RestMethod -Method Post -Uri $url -Headers $headers -Body ([System.Text.Encoding]::UTF8.GetBytes($body))

Upvotes: 2

Related Questions