Object

play.api.libs.ws

WS

Related Doc: package ws

Permalink

object WS

Asynchronous API to to query web services, as an http client.

Usage example:

class MyService @Inject() (ws: WSClient) {
  ws.url("http://example.com/feed").get()
  ws.url("http://example.com/item").post("content")
}

When greater flexibility is needed, you can also create clients explicitly and pass them into WS:

import play.api.libs.ws._
import play.api.libs.ws.ahc._
import akka.stream.Materializer
import play.api.ApplicationLifecycle
import javax.inject.Inject
import scala.concurrent.Future

class MyService @Inject() (lifecycle: ApplicationLifecycle)(implicit mat: Materializer) {
  private val client = new AhcWSClient(new AhcConfigBuilder().build())
  client.url("http://example.com/feed").get()
  lifecycle.addStopHook(() =>
    // Make sure you close the client after use, otherwise you'll leak threads and connections
    client.close()
    Future.successful(())
  }
}

Or call the client directly:

import com.typesafe.config.ConfigFactory
import play.api.libs.ws._
import play.api.libs.ws.ahc._

val configuration = play.api.Configuration(ConfigFactory.parseString(
"""
  |play.ws.ssl.trustManager = ...
""".stripMargin))
val parser = new DefaultWSConfigParser(configuration, app.classloader)
val builder = new AhcConfigBuilder(parser.parse())
val secureClient: WSClient = new AhcWSClient(builder.build())
val response = secureClient.url("https://secure.com").get()
secureClient.close() // must explicitly manage lifecycle

The value returned is a Future[WSResponse], and you should use Play's asynchronous mechanisms to use this response.

Annotations
@deprecated
Deprecated

(Since version 2.5.0) Inject WSClient into your component

Source
WS.scala
Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. WS
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def clientUrl(url: String)(implicit client: WSClient): WSRequest

    Permalink

    Prepares a new request using an implicit client.

    Prepares a new request using an implicit client. The client must be in scope and configured, i.e.

    implicit val sslClient = new play.api.libs.ws.ahc.AhcWSClient(sslBuilder.build())
    WS.clientUrl("http://example.com/feed")
    url

    the URL to request

    client

    the client to use to make the request.

  6. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  7. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  8. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  9. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  10. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  11. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  12. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  13. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  14. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  15. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  16. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  17. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  18. def url(magnet: WSRequestMagnet): WSRequest

    Permalink

    Prepares a new request using a provided magnet.

    Prepares a new request using a provided magnet. This method gives you the ability to create your own URL implementations at the cost of some complexity.

    object PairMagnet {
      implicit def fromPair(pair: Pair[WSClient, java.net.URL]) =
        new WSRequestHolderMagnet {
          def apply(): WSRequestHolder = {
            val (client, netUrl) = pair
            client.url(netUrl.toString)
          }
       }
    }
    import scala.language.implicitConversions
    val client = WS.client
    val exampleURL = new java.net.URL("http://example.com")
    WS.url(client -> exampleURL).get()
    magnet

    a magnet pattern.

    See also

    The magnet pattern

  19. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  22. def wsapi(implicit app: Application): WSAPI

    Permalink
    Attributes
    protected[play]

Deprecated Value Members

  1. def client(implicit app: Application): WSClient

    Permalink

    Retrieves or creates underlying HTTP client.

    Retrieves or creates underlying HTTP client. Note that due to the Plugin architecture, an implicit application must be in scope. Most of the time you will want the current app:

    val client = WS.client(app)
    Annotations
    @deprecated
    Deprecated

    (Since version 2.5.0) Inject WSClient into your component

  2. def url(url: String)(implicit app: Application): WSRequest

    Permalink

    Prepares a new request using an implicit application.

    Prepares a new request using an implicit application. This creates a default client, which you can then use to construct a request.

    WS.url("http://localhost/")(app).get()
    url

    the URL to request

    app

    the implicit application to use.

    Annotations
    @deprecated
    Deprecated

    (Since version 2.5.0) Inject WSClient into your component

Inherited from AnyRef

Inherited from Any

Ungrouped