Search code examples
scalahttp4s

http4s client create Uri from a string


I wrote the following code using http4s client library

import org.http4s.UrlForm
import org.http4s.Uri
import org.http4s.client.blaze.SimpleHttp1Client
import org.http4s.dsl._
import org.http4s.client._

val requestUrl = s"$url/foo/bar"
val client = SimpleHttp1Client()
val uri = Uri.fromString(requestUrl).valueOr(throw _) 
val req = POST(uri, UrlForm("name" -> "foo"))
val result = client.expect[String](req).run
println(result)

But this gets an exception

[error] Exception in thread "main" java.lang.NoSuchMethodError: scalaz.syntax.std.package$option$.ToOptionIdOps(Ljava/lang/Object;)Lscalaz/syntax/std/OptionIdOps;
[error]     at org.http4s.parser.Rfc3986Parser$class.HierPart(Rfc3986Parser.scala:33)
[error]     at org.http4s.parser.RequestUriParser.HierPart(RequestUriParser.scala:9)
[error]     at org.http4s.parser.Rfc3986Parser$class.AbsoluteUri(Rfc3986Parser.scala:21)
[error]     at org.http4s.parser.RequestUriParser.AbsoluteUri(RequestUriParser.scala:9)
[error]     at org.http4s.parser.Rfc3986Parser$class.Uri(Rfc3986Parser.scala:18)

My sbt dependencies are

libraryDependencies ++= Seq(
   "org.http4s" % "http4s-core_2.11" % "0.15.3"
   "org.http4s" % "http4s-blaze-client_2.11" % "0.15.3"
   "org.http4s" %% "http4s-argonaut" % "0.15.3"
   "org.http4s" % "http4s-dsl_2.11" % "0.15.3"
   "org.http4s" %% "http4s-client" % "0.15.3"
)

Solution

  • I cleaned them up a bit for you:

    libraryDependencies ++= Seq(
      "org.http4s" %% "http4s-core" % "0.15.3",
      "org.http4s" %% "http4s-blaze-client" % "0.15.3",
      "org.http4s" %% "http4s-argonaut" % "0.15.3",
      "org.http4s" %% "http4s-dsl" % "0.15.3",
      "org.http4s" %% "http4s-client" % "0.15.3"
    )
    
    scalaVersion := "2.11.8"
    

    I can't reproduce it with that. I'd install https://github.com/jrudolph/sbt-dependency-graph and see if you have some libraries that depends on scalaz 7.1/7.2 - here's the matrix

    For URI handling, there's a tiny bit of doc.