Skip to content

Instantly share code, notes, and snippets.

@evbruno
Created February 25, 2016 18:34
Show Gist options
  • Save evbruno/d2b1b111d76e1986008b to your computer and use it in GitHub Desktop.
Save evbruno/d2b1b111d76e1986008b to your computer and use it in GitHub Desktop.
Posting a JSON content with http4s / scalaz
// build.sbt
// "org.http4s" %% "http4s-client" % "0.12.3",
// "org.http4s" %% "http4s-blaze-client" % "0.12.3",
// "org.http4s" %% "http4s-dsl" % "0.12.3",
import org.http4s.MediaType._
import org.http4s.client._
import org.http4s.client.blaze.{defaultClient => client}
import org.http4s.dsl._
import org.http4s.headers._
import scalaz.{-\/, \/-}
object HttpApp extends App {
val jsonContent =
"""
|{
| "foo": "bar"
|}
""".stripMargin
val jsonType = Some(`Content-Type`(`application/json`))
val baseUri = uri("http://localhost:8080/api")
val req = POST(baseUri) withBody jsonContent withContentType jsonType
val task = client.fetchAs[String](req)
task.runAsync {
case -\/(t) => t.printStackTrace()
case \/-(r) => println("Task done:" + r)
}
println("sleeping...")
Thread.sleep(5000)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment