Git Product home page Git Product logo

Comments (10)

hseeberger avatar hseeberger commented on August 15, 2024

Very interesting! Let me play with this a little, which might take some time, because I'm on vacation.

from akka-http-json.

LukaJCB avatar LukaJCB commented on August 15, 2024

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.

hseeberger avatar hseeberger commented on August 15, 2024

Where? Client or server?

from akka-http-json.

LukaJCB avatar LukaJCB commented on August 15, 2024

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.

hseeberger avatar hseeberger commented on August 15, 2024

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.

LukaJCB avatar LukaJCB commented on August 15, 2024

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.

hseeberger avatar hseeberger commented on August 15, 2024

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.

hseeberger avatar hseeberger commented on August 15, 2024

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.

LukaJCB avatar LukaJCB commented on August 15, 2024

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.

hseeberger avatar hseeberger commented on August 15, 2024

No worries. It was quite educational.

from akka-http-json.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.