How do i use play ws library in normal sbt project instead of play?

10,846

Solution 1

To use play-ws outside of play see "Using WSClient" section of documentation: http://www.playframework.com/documentation/2.3.x/ScalaWS

val builder = new com.ning.http.client.AsyncHttpClientConfig.Builder()
val client = new play.api.libs.ws.ning.NingWSClient(builder.build())
val response = client.url(url).get()

Solution 2

Usage in 2.4.x

import play.api.libs.ws.ning.NingWSClient   

val wsClient = NingWSClient()
wsClient.url("http://wwww.something.com").get()

build.sbt :

libraryDependencies += "com.typesafe.play" %% "play-ws" % "2.4.3"

Usage in 2.5.x

import play.api.libs.ws.ahc.AhcWSClient

implicit val actorSystem = ActorSystem()
implicit val materializer = ActorMaterializer()
wsClient.url("http://wwww.something.com").get()

//at the very end, to shutdown stuff cleanly :
wsClient.close()
actorSystem.terminate()

build.sbt :

libraryDependencies += "com.typesafe.play" %% "play-ws" % "2.5.4"

Logs

As someone noted in the comment, by default you might get a bunch of verbose logs coming from the underlying async-http-client. One way to fix it is to start configuring a logback.xml, and placing it in src/main/resources

<configuration>

<conversionRule conversionWord="coloredLevel" converterClass="play.api.Logger$ColoredLevel" />

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <!-- The logging pattern, you might want to adapt it -->
        <pattern>%d %coloredLevel %t - %logger - %message%n%xException</pattern>
    </encoder>
</appender>

<!-- Here you can change the levels of specific loggers -->
<logger name="somelogger" level="INFO" />

<!-- Default logging level for every logger -->
<root level="ERROR">
    <appender-ref ref="STDOUT" />
</root>

</configuration>
Share:
10,846
Nagarjuna Pamu
Author by

Nagarjuna Pamu

Scala, Kotlin, Java, Docker, Kubernetes, PostgreSQL, Kafka, Cassandra, Event sourcing, Microservices.

Updated on June 16, 2022

Comments

  • Nagarjuna Pamu
    Nagarjuna Pamu almost 2 years

    When i tried using Play WS library in a normal sbt project instead of play project I was bound to use play.api.Play.current and got java.lang.RuntimeException: "There is no started application" when tried to run the application.

  • Chris W.
    Chris W. over 9 years
    What is the dependency-line needed for one's build.sbt/build.scala file? Is a special resolver necessary?
  • planetenkiller
    planetenkiller over 9 years
    try (untested): resolvers += "Typesafe Releases" at "https://repo.typesafe.com/typesafe/releases/" and "com.typesafe.play" %% "play-ws" % "2.3.7". (small note: play-ws has a dependency to play so you will have the whole play framwork in your project)
  • plamb
    plamb over 8 years
    Does anyone know how to turn off WS's verbose logging (printing of all the response headers) in this scenario (i.e. using it outside of play). It makes it very hard to debug anything
  • Felipe
    Felipe about 8 years
    Thanks. posting a link to a very useful comparison of HTTP toolkits for Scala: implicitdef.com/2015/11/19/…
  • ses
    ses about 8 years
    seems deprecated already. they recommended now:play.api.libs.ws.ahc.AhcWSClient
  • Andy Hayden
    Andy Hayden over 7 years
    I thought I used to be able to do this in sbt console but I keep seeing: res23: scala.concurrent.Future[play.api.libs.ws.WSResponse] = List() as the return from get.., !
  • stanislav.chetvertkov
    stanislav.chetvertkov about 7 years
    ^^^ try using Await.result(), List() means that there is no value yet