Character encoding with Invoke-RestMethod


I am updating data via Invoke-RestMethod (PUT) and the endpoint is returning a 400 error for any records including the French character “é”. I think this something to do with the way PowerShell is encoding data before executing the command.

E.g. JSON includes: [pre]{… “surname”: “Le Touzé” …}[/pre]

Server response would be something like: [pre] (PUT). The remote server returned an error: (400) Bad Request.[/pre]

Interestingly, the exact same request works fine from a REST client, such as Insomnia or Postman.

Any thoughts, or similar experiences?


Don’t have anything to test with but the default encoding is ‘ISO-8859-1’ as far as I found from google.

There are a number of issues raised on github.
Some solutions suggest setting the charset in the header.

Content-Type = ‘application/json; charset=utf-8’

You may need to add the switch -SkipHeaderValidation according to one of the issues on github.

You can also encode the strings to something else e.g. UTF-8
$body = [System.Text.Encoding]::UTF8.GetBytes($json)

If it works correctly in Postman I would think that you can check the headers in the response.
Just to verify which encoding it’s using etc.