1

我使用 http4s 库编写了这个简单的 scala 代码

import org.http4s.client.blaze._

object ScalaHttpTest extends App {
  val c = PooledHttp1Client()
  val rTask = c.expect[String]("""http://localhost:50070/webhdfs/v1/user/?op=LISTSTATUS""")
  val x = rTask.attemptRun.toEither
  if (x.isRight) println(x.right)
  if (x.isLeft) println(x.left)
  c.shutdownNow()
}

如果我将我的 URL 复制粘贴到 chrome、postman、curl 中,它可以完美运行并显示输出

HTTP/1.1 200 OK
Cache-Control: no-cache
Expires: Sun, 11 Sep 2016 04:11:26 GMT
Date: Sun, 11 Sep 2016 04:11:26 GMT
Pragma: no-cache
Expires: Sun, 11 Sep 2016 04:11:26 GMT
Date: Sun, 11 Sep 2016 04:11:26 GMT
Pragma: no-cache
Content-Type: application/json
Transfer-Encoding: chunked
Server: Jetty(6.1.26.cloudera.4)

{"FileStatuses":{"FileStatus":[

]}}

这是来自 chrome 的屏幕截图

在此处输入图像描述

但我的代码抛出错误信息

[info] Compiling 1 Scala source to /home/user/MyProjects/ScalaHttpTest/target/scala-2.11/classes...
[info] Running com.abhi.ScalaHttpTest 
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
LeftProjection(Left(org.http4s.client.UnexpectedStatus: unexpected HTTP status: 500 Internal Server Error))
[success] Total time: 4 s, completed Sep 10, 2016 11:09:40 PM

Process finished with exit code 0
4

1 回答 1

4

expectString. 在 HTTP 术语中,这意味着您假设一个Content-Type. text/*但是,该服务(正确地)返回 a Content-Typeof application/json。要处理这个问题,您需要使用 http4s 的 JSON 集成子项目之一,例如它的Argonaut支持expect[Json]

于 2016-09-11T16:04:22.200 回答