Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example: Using Play Framework's WS library standalone
name := "WS Standalone Example"
version := "1.0"
scalaVersion := "2.11.5"
libraryDependencies ++= Seq(
"com.typesafe.play" %% "play-ws" % "2.4.0-M2"
)

This gist provides a simple example of how to use Play's WS library in a standalone application (external to Play).

build.sbt - SBT build file that includes the WS library WSStandaloneTest.scala - A simple example that utilizes WS to invoke an HTTP GET request

import com.ning.http.client.AsyncHttpClientConfig
import play.api.libs.ws.ning._
import play.api.libs.ws._
// provide an execution context
import scala.concurrent.ExecutionContext.Implicits.global
object WSStandaloneTest {
def main(args: Array[String]) {
// set up the client
val config = new NingAsyncHttpClientConfigBuilder(DefaultWSClientConfig()).build
val builder = new AsyncHttpClientConfig.Builder(config)
val client = new NingWSClient(builder.build)
// execute a GET request
val response = client.url("http://www.example.com").get
// print the response body
response.foreach(r => {
println(r.body)
// not the best place to close the client,
// but it ensures we dont close the threads before the response arrives
// Good enough for the gist :-D
client.close()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment