Skip to content

Invoke-RestMethod/Invoke-WebRequest should set charset=utf-8 for application/json by default #10971

Closed

Description

@rjmholt

Recently had an issue trying to use a GitHub API when I put emojis in the POST body:

$body = ConvertFrom-Json @{
    tag_name = "tag"
    name = "demo"
    body = "I like 🐛s"
    draft = $true
    prerelease = $true
}

$uri = "https://api.github.com/repos/me/bughunter/releases"
$headers = @{
    GitHubToken = $myToken
    Accept = "application/vnd.github.v3+json"
}

Invoke-RestMethod -Method Post -Uri $uri -Body $body -Headers $headers

Sending this published a draft with the description:

I like ??s

Took me a while to work out I needed -ContentType "application/json; charset=utf-8", which is fine. Except everything else defaulted (as expected) to UTF-8, so I assumed this would too.

Activity

  1. rjmholt commented on Nov 1, 2019

    @rjmholt
    CollaboratorAuthor

    Worth noting that this is probably classable as a breaking change, but if we're always converting upper ASCII to ? then I'm not sure what we'd be breaking.

  2. SteveL-MSFT commented on Nov 1, 2019

    @SteveL-MSFT
    Member

    Given that our console and output encoding default to utf-8, it seems like the webcmdlets should also follow with utf-8 as default charset unless overridden. cc Mark Kraus (@markekraus)

  3. he852100 commented on Nov 3, 2019

    @he852100

    Xml is not recognized

    PS /> (irm 'https://storage.live.com/items/<redacted>?&authkey=<redacted>')
     <?xml version="1.0" encoding="utf-8"?>
  4. SteveL-MSFT commented on Nov 4, 2019

    @SteveL-MSFT
    Member

    he852100 I edited your example removing sensitive information. If you're referring to the first couple of characters, it appears to be a malformed BOM. How was that document saved to live.com? Was it UTF-8 encoded with a BOM?

  5. he852100 commented on Nov 5, 2019

    @he852100

    Steve Lee (@SteveL-MSFT) It's the data of the website.I didn't create it.

  6. CodeShane commented on May 11, 2021

    @CodeShane

    That's a UTF8 BOM encoded as ASCII or other non- Saw it take down a production service a few jobs ago when an xml feed encoded in UTF-8 decided to also send the optional BOM.

     U+FEFF Byte Order Mark

  7. ghost added
    In-PRIndicates that a PR is out for the issue
    on Oct 4, 2022
  8. ghost removed
    In-PRIndicates that a PR is out for the issue
    on Nov 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-PRIndicates that a PR is out for the issueIssue-Enhancementthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions