Comments (10)
Very interesting! Let me play with this a little, which might take some time, because I'm on vacation.
from akka-http-json.
I've found that you can very simply solve this problem by specifying that the contentType
of the HttpEntity
be `application/json`.withParams(Map("charset" -> "utf-8"))
instead of just `application/json`
.
from akka-http-json.
Where? Client or server?
from akka-http-json.
On the server. Something like
val body = Foo("München").asJson.noSpaces
val entity = HttpEntity(`application/json`.withParams(Map("charset" -> "utf-8")), body)
complete(entity)
from akka-http-json.
After reading up on JSON (e.g. RFC 7159 and SO) I think that the current implementation is just fine, because application/json
is defined as MediaType.WithFixedCharset
(of course UTF-8 is used) and UTF-8 is defined as the default encoding for JSON, hence setting the charset in the response header explicitly is redundant. The client needs to be changed to assume UTF-8 if not indicated otherwise.
from akka-http-json.
I see your points, however, I'm not sure if that's the way it should be dealt with. I don't think we can change browser internals to assume UTF-8 (maybe there's something I don't know about, the only thing I can think about is add a request header to accept utf-8
only).
Moreover, if I look at any other public api out there they all include the response header Content-Type: application/json; charset=utf-8
, I concur that "all the others do it" is not the greatest argument, but I believe it shouldn't stray to far from the others for the sake of compatibility.
from akka-http-json.
You're speaking about a browser, but browsers are for HTML. Have you tried your example with a HTTP client, e.g. curl or httpie?
The closest others in this case are Akka and the other libraries supporting JSON encoding. AFAIK none sends that header.
from akka-http-json.
I tried to reproduce this issue but could not. Neither with httpie nor with Safari.
Now here's a rude assumption: when you implemented the server, are you sure the source files are UTF-8 encoded? Please double check.
from akka-http-json.
Sorry for getting back to you late, I was on vacation as well.
Now here's something odd, I double checked the encoding of the source file and it is indeed UTF-8, however I checked again with httpie and this time it seems to have worked, even though I could have sworn it did not the last time I tried. Browser(Chrome) still gives me wrong answer though. I concur with your point that they are for HTML. I was assigned to fix the issue by a colleague of mine, so I'm going to have to check with him for which client he was using. Thanks again for your time! :)
from akka-http-json.
No worries. It was quite educational.
from akka-http-json.
Related Issues (20)
- GPG key not found during release HOT 3
- Scala 2.13 support HOT 9
- Non Http related Akka dependencies should be marked as provided HOT 2
- Release tag for akka 10.1.8 HOT 1
- Unmarshalling fails for nullable json with circe HOT 1
- Upgrade akka-http-circe to Scala 2.13 HOT 5
- is it possible to allow any content type header when unmarshalling in play ? HOT 1
- Unable to import support mixins HOT 8
- support for akka 2.6.0 HOT 4
- Intellij warnings using Json4sSupport using parameters directive with akka-http HOT 3
- akka-http-jackson: defaultObjectMapper should be def instead of val HOT 1
- publish release to include ninny HOT 2
- cannot Unmarshal from String HOT 2
- zio-json support HOT 1
- Compile fails in scala 3 with implicit errors for circe HOT 3
- implicit summoning of JsArray ToEntityMarshaller consumes all compiler memory
- Symbol 'type akka.stream.scaladsl.Flow' is missing from the classpath. HOT 1
- PlayJson: ToResponseMarshaller does not work when a Format[A] is given instead a Writes[A]
- Cross-compile modules for Scala 3 HOT 5
- Support for Pekko HTTP? HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from akka-http-json.