Search code examples
scalahttp4s

Very simple call cannot be made using http4s


I wrote this simple scala code using http4s library

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()
}

If I copy paste my URL in chrome, postman, curl it works perfectly and it shows the output

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":[

]}}

here is a screenshot from chrome

enter image description here

But my code throws error message

[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

Solution

  • You're expecting a String. In HTTP terms, that means you're assuming a Content-Type of text/*. The service, however, is (correctly) returning a Content-Type of application/json. To handle this, you'll want to use one of http4s' JSON integration subprojects, such as its Argonaut support, and expect[Json] instead.