Webservice usage with play WSClient
Remarks#
Link to official documentation: https://www.playframework.com/documentation/2.5.x/ScalaWS
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 play.api.libs.ws.WSClient
import scala.concurrent.{ExecutionContext, Future}
class MyClass @Inject() (
wsClient: WSClient
)(implicit ec: ExecutionContext){
def doGetRequest(): Future[String] = {
wsClient
.url("https://www.google.com")
.get()
.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
response.body
} else
throw new IllegalStateException(s"We received status ${response.status}")
}
}
}