使用http4s无法进行非常简单的调用

时间:2016-09-11 04:13:10

标签: scala http4s

我使用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()
}

如果我复制粘贴我的网址在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的截图

enter image description here

但我的代码会抛出错误消息

[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

1 个答案:

答案 0 :(得分:4)

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