question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
SteveL-MSFTcommented, Nov 1, 2019

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 @markekraus

0reactions
CodeShanecommented, May 11, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring set default Content-type to "application/json;charset ...
The simplest solution for default content type charset, that I found, ... GET, produces = { "application/json; charset=utf-8" }) public ...
Read more >
application/json character set with entity(as[String]) is not ...
Shouldn't the default charset for "application/json" be UTF-8 also when decoding the entity? I recall there was a similar issue with JSON ...
Read more >
Benefit to specifying JSON Content-Type character set?
According to the RFC for the application/json Media Type: JSON text SHALL be encoded in Unicode. The default encoding is UTF-8. Since the...
Read more >
Use JSON content-type for key-values - Azure App ...
Data is stored in App Configuration as key-values, where values are treated as the string type by default. However, you can specify a...
Read more >
How to customize character encoding with System.Text.Json
By default, the encoder is initialized with the BasicLatin range. To serialize all language sets without escaping, use UnicodeRanges.All.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found