Synchronous HTTP call in Scala -
i need perform single synchronous http post call: create http post request data, connect server, send request, receive response, , close connection. important release all resources used call.
now doing in java apache http client. how can scala dispatch
library ?
something should work (haven't tested though)
import dispatch._, defaults._ import scala.concurrent.future import scala.concurrent.duration._ def postsync(path: string, params: map[string, any] = map.empty): either[java.lang.throwable, string] = { val r = url(path).post << params val future = http(r ok as.string).either await.result(future, 10.seconds) }
(i'm using https://github.com/dispatch/reboot example)
you explicitly wait result of future, either string
or exception.
and use like
postsync("http://api.example.com/a/resource", map("param1" -> "foo") match { case right(res) => println(s"success! result $res") case left(e) => println(s"woops, went wrong: ${e.getmessage}") }
Comments
Post a Comment