Character encoding with Invoke-RestMethod

Hello

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]https://path.to.server/api/resource/123456789 (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?

Thanks

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.

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

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
[pre]
$body = [System.Text.Encoding]::UTF8.GetBytes($json)
[/pre]

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.