Scalatra,使用 Gzip 编码处理 POST 请求
Scalatra, Handling POST request with Gzip encoding
我有一个带有 POST 方法的 Scalatra API,该方法由发送 gzip 编码消息的外部 API 调用。但是我无法读取post请求的body,当我尝试读取POST的body或尝试解压时,出现异常:
MalformedInputException: Input length = 1.
如果我将传入的 POST 请求路由到 fiddler,fiddler 会正确处理消息并声明消息已编码,解码后,我可以在 body 中看到正确的消息请求
以下是我尝试过的方法:
val body = new GZIPInputStream(new ByteArrayInputStream(request.body.getBytes))
同样的异常被抛出:
logger.info(s"Response body..." + request.body )
我也尝试将 body 读取为 json,但它 returns JNothing:
logger.info(s"Response body...\n${parsedBody}" )
这里是 POST headers:
(Expect,100-continue)
(Connection,keep-alive)
(Content-Encoding,gzip)
(Content-Type,application/json)
(Content-Length,3500)
这里是栈道的一部分:
Parsing the request body failed, because:
java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(Unknown Source) ~[na:1.8.0_45]
at sun.nio.cs.StreamDecoder.implRead(Unknown Source) ~[na:1.8.0_45]
at sun.nio.cs.StreamDecoder.read(Unknown Source) ~[na:1.8.0_45]
at java.io.InputStreamReader.read(Unknown Source) ~[na:1.8.0_45]
at java.io.BufferedReader.read1(Unknown Source) ~[na:1.8.0_45]
at java.io.BufferedReader.read(Unknown Source) ~[na:1.8.0_45]
at java.io.Reader.read(Unknown Source) ~[na:1.8.0_45]
at scala.io.BufferedSource.mkString(BufferedSource.scala:96) ~[scala-library-2.11.6.jar:na]
at org.scalatra.servlet.RichRequest$$anonfun$body.apply(RichRequest.scala:181) ~[scalatra_2.11-2.3.0.jar:2.3.0]
at org.scalatra.servlet.RichRequest$$anonfun$body.apply(RichRequest.scala:176) ~[scalatra_2.11-2.3.0.jar:2.3.0]
at scala.Option.getOrElse(Option.scala:121) [scala-library-2.11.6.jar:na]
at org.scalatra.servlet.RichRequest.body(RichRequest.scala:176) ~[scalatra_2.11-2.3.0.jar:2.3.0]
at org.scalatra.json.JsonSupport$class.parseRequestBody(JsonSupport.scala:29) [scalatra-json_2.11-2.3.0.jar:2.3.0]
at impala.api.GreetingController.parseRequestBody(GreetingController.scala:13) [classes/:na]
可以在 scalatra 用户组中找到解决方案:
https://groups.google.com/forum/#!topic/scalatra-user/2vBBA2-CzDM
我有一个带有 POST 方法的 Scalatra API,该方法由发送 gzip 编码消息的外部 API 调用。但是我无法读取post请求的body,当我尝试读取POST的body或尝试解压时,出现异常:
MalformedInputException: Input length = 1.
如果我将传入的 POST 请求路由到 fiddler,fiddler 会正确处理消息并声明消息已编码,解码后,我可以在 body 中看到正确的消息请求
以下是我尝试过的方法:
val body = new GZIPInputStream(new ByteArrayInputStream(request.body.getBytes))
同样的异常被抛出:
logger.info(s"Response body..." + request.body )
我也尝试将 body 读取为 json,但它 returns JNothing:
logger.info(s"Response body...\n${parsedBody}" )
这里是 POST headers:
(Expect,100-continue)
(Connection,keep-alive)
(Content-Encoding,gzip)
(Content-Type,application/json)
(Content-Length,3500)
这里是栈道的一部分:
Parsing the request body failed, because:
java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(Unknown Source) ~[na:1.8.0_45]
at sun.nio.cs.StreamDecoder.implRead(Unknown Source) ~[na:1.8.0_45]
at sun.nio.cs.StreamDecoder.read(Unknown Source) ~[na:1.8.0_45]
at java.io.InputStreamReader.read(Unknown Source) ~[na:1.8.0_45]
at java.io.BufferedReader.read1(Unknown Source) ~[na:1.8.0_45]
at java.io.BufferedReader.read(Unknown Source) ~[na:1.8.0_45]
at java.io.Reader.read(Unknown Source) ~[na:1.8.0_45]
at scala.io.BufferedSource.mkString(BufferedSource.scala:96) ~[scala-library-2.11.6.jar:na]
at org.scalatra.servlet.RichRequest$$anonfun$body.apply(RichRequest.scala:181) ~[scalatra_2.11-2.3.0.jar:2.3.0]
at org.scalatra.servlet.RichRequest$$anonfun$body.apply(RichRequest.scala:176) ~[scalatra_2.11-2.3.0.jar:2.3.0]
at scala.Option.getOrElse(Option.scala:121) [scala-library-2.11.6.jar:na]
at org.scalatra.servlet.RichRequest.body(RichRequest.scala:176) ~[scalatra_2.11-2.3.0.jar:2.3.0]
at org.scalatra.json.JsonSupport$class.parseRequestBody(JsonSupport.scala:29) [scalatra-json_2.11-2.3.0.jar:2.3.0]
at impala.api.GreetingController.parseRequestBody(GreetingController.scala:13) [classes/:na]
可以在 scalatra 用户组中找到解决方案:
https://groups.google.com/forum/#!topic/scalatra-user/2vBBA2-CzDM