akka http,字符集头,utf-8问题

Ale*_*rov 3 scala character-encoding akka spray akka-http

我有这条路线:

path("rus") {
  complete("??????!")
}
Run Code Online (Sandbox Code Playgroud)

当我使用浏览器(chrome)进入/ rus时,得到以下输出:

“ !!”!

为什么?响应头是:

HTTP/1.1 200 OK
Server: akka-http/2.4.10
Date: Mon, 10 Oct 2016 22:31:53 GMT
Content-Type: application/json
Content-Length: 15
Run Code Online (Sandbox Code Playgroud)

我曾经使用过喷雾,但现在我想使用akka http,我没有遇到过这样的问题。

当我卷曲此路径时,我得到正常输出

$ curl http://localhost:9010/rus
"??????!"
Run Code Online (Sandbox Code Playgroud)

我看到响应标头'Content-Type'应该是'application / json; charset = utf-8'但缺少charset ...

Ale*_*rov 5

我找到了 withParams 的方法。现在它起作用了

HttpEntity(ContentType(MediaTypes.`application/json`.withParams(Map("charset" -> "utf-8"))), "??????".getBytes("UTF-8"))
Run Code Online (Sandbox Code Playgroud)


jru*_*lph 5

不幸的是,这是铬的错误。根据其IANA注册charsetapplication/json内容类型的参数必须忽略:

注意:没有为该注册定义“字符集”参数。确实添加一个对符合条件的收件人没有任何影响。