Skip to content

Instantly share code, notes, and snippets.

@mpkocher
Last active October 2, 2019 15:00
Show Gist options
  • Save mpkocher/21a9fafdbd910e561fe0d3554881a32c to your computer and use it in GitHub Desktop.
Save mpkocher/21a9fafdbd910e561fe0d3554881a32c to your computer and use it in GitHub Desktop.
akka-http Uri Example (Ammonite output)
@ import akka.http.scaladsl.model.Uri
import akka.http.scaladsl.model.Uri
@ val u = Uri("http://smrtlink-bihourly:8081")
u: Uri = Uri("http", Authority(NamedHost("smrtlink-bihourly"), 8081, ""), , None, None)
@ val p1 = Uri.Path("root") / "alpha" / "beta"
p1: Uri.Path = Segment("root", Slash(Segment("alpha", Slash(Segment("beta", )))))
@ val p2 = p1 / "gamma"
p2: Uri.Path = Segment("root", Slash(Segment("alpha", Slash(Segment("beta", Slash(Segment("gamma", )))))))
@ p2.toString
res4: String = "root/alpha/beta/gamma"
@ val p3 = Uri.Path./ ++ p2
p3: Uri.Path = Slash(Segment("root", Slash(Segment("alpha", Slash(Segment("beta", Slash(Segment("gamma", ))))))))
@ p3.toString
res6: String = "/root/alpha/beta/gamma"
@ val u2 = u.copy(path = p3)
u2: Uri = Uri(
"http",
Authority(NamedHost("smrtlink-bihourly"), 8081, ""),
Slash(Segment("root", Slash(Segment("alpha", Slash(Segment("beta", Slash(Segment("gamma", )))))))),
None,
None
)
@ u2.toString
res8: String = "http://smrtlink-bihourly:8081/root/alpha/beta/gamma"
@ val q1 = Uri.Query("a" -> "alpha", "b" -> "beta")
q1: Uri.Query = Uri.Query.Cons(("a", "alpha"), ("b", "beta"))
@ val u3 = u2.withQuery(q1)
u3: Uri = Uri(
"http",
Authority(NamedHost("smrtlink-bihourly"), 8081, ""),
Slash(Segment("root", Slash(Segment("alpha", Slash(Segment("beta", Slash(Segment("gamma", )))))))),
Some("a=alpha&b=beta"),
None
)
@ u3.toString
res11: String = "http://smrtlink-bihourly:8081/root/alpha/beta/gamma?a=alpha&b=beta"
@ val q2 = Uri.Query("a" -> "alpha", "b" -> "beta x+y+z")
q2: Uri.Query = Uri.Query.Cons(("a", "alpha"), ("b", "beta x+y+z"))
@ u2.withQuery(q2).toString
res13: String = "http://smrtlink-bihourly:8081/root/alpha/beta/gamma?a=alpha&b=beta+x%2By%2Bz"
@ // Notice the URL encoding for free
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment