playframework Webservice usage with play WSClient Basic usage (Scala)


HTTP requests are made through the WSClient class, which you can use as an injected parameter into your own classes.

import javax.inject.Inject


import scala.concurrent.{ExecutionContext, Future}

class MyClass @Inject() (
  wsClient: WSClient
)(implicit ec: ExecutionContext){
  def doGetRequest(): Future[String] = {
      .map { response =>
      // Play won't check the response status,
      // you have to do it manually
      if ((200 to 299).contains(response.status)) {
        println("We got a good response")
        // response.body returns the raw string
        // response.json could be used if you know the response is JSON
      } else
        throw new IllegalStateException(s"We received status ${response.status}")