StopKran
StopKran

Reputation: 395

Play2, scala: how to use controller result from another controller?

I have an application, that used play 2.3.8, scala and (if it matter) play-auth.

Have a controller, with method:

def foo(id: Long) = StackAction(AuthorityKey -> Everybody) { implicit request =>
  //code forming json   
  Ok(json) 
}

How can I get that json from another controller? I try something, but without success:

def bar(id : Long) = StackAction(AuthorityKey -> Everybody){ implicit request =>
  val futureResponse = AnotherController.foo(id).apply(request)
  val result = Await.result(futureResponse, Timeout(5, TimeUnit.SECONDS).duration)
  Logger.debug("_______________________" + result.body) //dont't know how to convert that to json
  //handle json there
  Ok(newResult)
}

How to do that right?

Upvotes: 0

Views: 183

Answers (1)

MrRontgen
MrRontgen

Reputation: 132

Try this

 def bar(id : Long) = StackAction(AuthorityKey -> Everybody){ implicit request =>

      val futureResponse: Future[JsValue] = 
          AnotherController.foo(id).apply(request).flatMap{ res =>
              res.body |>>> Iteratee.consume[Array[Byte]]()
          }.map(bytes => Json.parse(new String(bytes,"utf-8")))

      val json = Await.result(futureResponse, Timeout(5, TimeUnit.SECONDS).duration)
      Logger.debug("_______________________" + json) //dont't know how to convert that to json
      //handle json there
      Ok(json)
    }

Upvotes: 0

Related Questions