Character encoding with Invoke-RestMethod

Welcome Forums General PowerShell Q&A Character encoding with Invoke-RestMethod

This topic contains 1 reply, has 2 voices, and was last updated by

2 months, 1 week ago.

  • Author
  • #160749

    Topics: 10
    Replies: 11
    Points: 122
    Rank: Participant


    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:

    {... "surname": "Le Touzé" ...}

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

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

    Any thoughts, or similar experiences?


  • #160997

    Topics: 12
    Replies: 231
    Points: 461
    Helping Hand
    Rank: Contributor

    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.

You must be logged in to reply to this topic.