"How are...">

POST request using play ws in Scala

I am using play-ws standalone to use the REST service in scala.

val data = Json.obj("message" -> "How are you?")
wsClient.url("http://localhost:5000/token").post(data).map { response =>
      val statusText: String = response.statusText
      println(response.body)
    }

When I run this, I get the following error,

Cannot find an instance of play.api.libs.json.JsObject to WSBody. Define a BodyWritable[play.api.libs.json.JsObject] or extend play.api.libs.ws.ahc.DefaultBodyWritables
    wsClient.url("http://localhost:5000/token").post(data).map { response =>

He reports to determine recordable. I have read the documentation but am not getting "BodyWritable". I am new to scala. Someone help me. Thanks in advance.

+7
source share
2 answers

You need to import BodyWritables for json objects, add the following import statements to the source file

import play.api.libs.ws.JsonBodyReadables._
import play.api.libs.ws.JsonBodyWritables._

For more information, see the official documentation.

+10
source

Scala Play 2.7.x (, ).

, asScala ws. :

  val data = Json.obj("message" -> "How are you?")
  ws
    .asScala()
    .url("http://someurl.com")
    .post(data)
    .map(response => {
      //do something with response
    })

: Scala Java.

0

All Articles