Created
February 25, 2016 18:34
-
-
Save evbruno/d2b1b111d76e1986008b to your computer and use it in GitHub Desktop.
Posting a JSON content with http4s / scalaz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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