无法使用 http4s 进行非常简单的调用
Very simple call cannot be made using 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()
}
如果我将我的 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
您正在 expect
String
。在 HTTP 术语中,这意味着您假设 Content-Type
为 text/*
。但是,该服务(正确地)返回 application/json
的 Content-Type
。要处理这个问题,您需要使用 http4s 的 JSON 集成子项目之一,例如它的 Argonaut 支持,而 expect[Json]
代替。
我使用 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
您正在 expect
String
。在 HTTP 术语中,这意味着您假设 Content-Type
为 text/*
。但是,该服务(正确地)返回 application/json
的 Content-Type
。要处理这个问题,您需要使用 http4s 的 JSON 集成子项目之一,例如它的 Argonaut 支持,而 expect[Json]
代替。