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.

application/json default charset is not UTF-8 when parsing request

See original GitHub issue

This is a follow up to #80. Apparently, responses were fixed, but there are still issues with the requests.

Request with Content-Type: application/json is decoded not as UTF-8, while request with Content-Type: application/json; charset=utf-8 is decoded correctly. Both has to behave the same and be decoded as UTF-8.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:15
  • Comments:30 (7 by maintainers)

github_iconTop GitHub Comments

29reactions
functionaldudecommented, Jan 29, 2019

Use this function if you just want to “have it work”:

/**
 * Receive the request as String.
 * If there is no Content-Type in the HTTP header specified use ISO_8859_1 as default charset, see https://www.w3.org/International/articles/http-charset/index#charset.
 * But use UTF-8 as default charset for application/json, see https://tools.ietf.org/html/rfc4627#section-3
 */
private suspend fun ApplicationCall.receiveTextWithCorrectEncoding(): String {
  fun ContentType.defaultCharset(): Charset = when (this) {
    ContentType.Application.Json -> Charsets.UTF_8
    else -> Charsets.ISO_8859_1
  }
  
  val contentType = request.contentType()
  val suitableCharset = contentType.charset() ?: contentType.defaultCharset()
  return receiveStream().bufferedReader(charset = suitableCharset).readText()
}

@cy6erGn0m Maybe you want to change the default implementation of receiveText to the implementation of receiveTextWithCorrectEncoding()?

3reactions
clydebarrowcommented, Feb 14, 2020

I have run into this problem as well. The receiveTextWithCorrectEncoding() work-around solved it for now, but it seems like it is still an issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What does "Content-type: application/json; charset=utf-8 ...
JSON text SHALL be encoded in Unicode. The default encoding is UTF-8. Since the first two characters of a JSON text will always...
Read more >
[play 2.6] charset removed with application/json content-type
When sending a request with the content type "text/plain;charset=UTF-8", request. ... to one of our application where we do not use the default...
Read more >
1266023 - JSON's default charset should be UTF-8
The server-side workaround is to get the web server to provide "Content-Type: application/json; charset=utf-8" in its responses instead of "Content-Type: ...
Read more >
Json – Default encoding of HTTP POST request with JSON body
JSON spec says that "JSON text SHALL be encoded in Unicode. The default encoding is UTF-8." HTTP spec says that "When no explicit...
Read more >
JSON to JSONX is not converting all UTF-8 characters - IBM
based on my test scenarios it is encode ISO-8859 chars not UTF-8 all characters, try with any payload with json element value other...
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