score:13

Accepted answer

EDIT for Play 2.5:

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import play.api.libs.ws._
import play.api.libs.ws.ahc.AhcWSClient

import scala.concurrent.Future

object Main {
  import scala.concurrent.ExecutionContext.Implicits._

  def main(args: Array[String]): Unit = {
    implicit val system = ActorSystem()
    implicit val materializer = ActorMaterializer()
    val wsClient = AhcWSClient()

    call(wsClient)
      .andThen { case _ => wsClient.close() }
      .andThen { case _ => system.terminate() }
  }

  def call(wsClient: WSClient): Future[Unit] = {
    wsClient.url("http://www.google.com").get().map { response =>
      val statusText: String = response.statusText
      println(s"Got a response $statusText")
    }
  }
}

Please see:

for more detailed examples of standalone WSClient usage. If you are migrating from earlier versions, see https://www.playframework.com/documentation/2.5.x/Migration25#Play-WS-upgrades-to-AsyncHttpClient-2

For Play 2.4:

Do not use raw AsyncHttpClientConfig.Builder for HTTPS -- it does not configure a secure SSLContext with hostname validation.

You can create a new WSClient instance using the following code:

import play.api.libs.ws.ning._
import play.api.libs.ws._

val config = new NingAsyncHttpClientConfigBuilder(DefaultWSClientConfig()).build()
val builder = new AsyncHttpClientConfig.Builder(config)
val wsClient:WSClient = new NingWSClient(builder.build())

Please note that this will start up threads which will not be closed until you close the client:

wsClient.underlying[NingWSClient].close()

and you may run into memory leaks if you don't close it.

score:4

A started PlayApplication contains a client instance, which WS.client simply points to it. Since you won't start a Play application, You have to create your own client, like this:

val client = {
  val builder = new com.ning.http.client.AsyncHttpClientConfig.Builder()
  new play.api.libs.ws.ning.NingWSClient(builder.build())
}
client.url("http://example.com/").get()

Have a look on my project for a similar usecase, I am using play-ws and play-json, without Play itself.

score:8

Play 2.4 makes it very easy to utilize WS in a standalone app.

The following gist provides a nice working example and the following blog post provides a nice explanation.

Here are the highlights.

Configure build.sbt

libraryDependencies ++= Seq(
  "com.typesafe.play" %% "play-ws" % "2.4.0-M2"
)

Initialize the WS client

val config = new NingAsyncHttpClientConfigBuilder(DefaultWSClientConfig()).build
val builder = new AsyncHttpClientConfig.Builder(config)
val client = new NingWSClient(builder.build)

Use WS

client.url("http://www.example.com").get

Release WS resources

client.close()

Related Query

More Query from same tag