0

I'm sending an http post request as such:

def Token(url: String, Id: String, key: String): String = {
  val body =
    s"""
      | "id": ${Id}
      | "key": ${key}
      |""".stripMargin

  val request = Http(url).postData(body)
    .header("content-type", "application/json")
    .option(HttpOptions.method("POST"))

  val response = request.execute()

}

My response body is in the form:

{
    "token": "xyz",
    "abc": "defgh"
}

I want to parse this response to get the value of "token" ("xyz") in Scala. How do you do this?

Mario Galic
  • 47,285
  • 6
  • 56
  • 98
codee
  • 31
  • 1
  • 4

1 Answers1

0

There's a syntax you can use on Play framework like this:

response =>
  val json = response.json
  println (json \ "error").asOpt[String]

You can read more about it here:

https://www.playframework.com/documentation/2.8.x/ScalaJsonHttp

Philluminati
  • 2,649
  • 2
  • 25
  • 32