Search code examples
javascalarestfunctional-programmingplayframework-2.6

Scala compile time error: No implicits found for parameter evidence$2: BodyWritable[Map[String, Object]]


I am working on scala play framework application. I am trying to call a web service API which takes request payload data as follows

{
    "toID": [
        "[email protected]",
        "[email protected]"
    ],
    "fromID": "[email protected]",
    "userID": "ervd12fdsfksdjnfn9832rbjfdsnf",
    "mailContent": "Dear Sir, ..."
}

And for this I am using following code

ws.url(Utils.messengerServiceUrl + "service/email")
          .post(
            Map("userID" -> userID, "mailContent" -> userData.message, "fromID" -> "[email protected]", "toID" -> userData.emails)).map { response =>
          println(response.body, response.status)
        }

So for this code, compiler is complaining about "toID" -> userData.emails saying No implicits found for parameter evidence$2: BodyWritable[Map[String, Object]]

So my question is how to send such data using WSClient?


Solution

  • You can do it with case class like that

    import play.api.libs.json._
    
    
    case class Message(toID: Seq[String], fromID: String, userID: String, mailContent: String)
    
    object Message {
      implicit val writes: Writes[Message] = Json.writes[Message]
    }
    

    Pay attention to the definition of the object Message with implicit writes