http4s 客户端从字符串创建 Uri

http4s client create Uri from a string

我使用 http4s 客户端库编写了以下代码

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)

但这有一个例外

[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)

我的 sbt 依赖项是

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"
)

我为你清理了一下:

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"

我无法用那个重现它。我会安装 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

对于 URI 处理,a tiny bit of doc