In Services, i m calling the endpoint service and got response as Future[] Result. I need to send the Future[] Result to Controllers. In Controllers i should respond the result via Ok(response).

//In Controllers i have to receive the response from Services and have to 
send the success or failure response.

def postLead = Action.async {  lead =>
   val isSuccess = service.postToFreedom(lead).map{ response =>
       Ok(response)
   }
}

//In Services,i am hitting the end point service and got the response as Future[]

private def postToFreedom(postXML:Request): Future[WSResponse] = {
   val leadXml = FreedomMortgageXML(postXML).toXml
   val response = ws.url(serviceEndpoint).withHeaders("Content-Type" -> "application/xml").post(leadXml)

   response.map { response =>
   response
}
1

There are 1 best solutions below

0
On

You need to convert your ws.WSResponse into a mvc.Result:

def postLead = Action.async {  lead =>
  service.postToFreedom(lead).map{ wsResponse =>
    val headers = wsResponse.allHeaders map {
      h => (h._1, h._2.head)
    }
    Result(ResponseHeader(wsResponse.status, headers), Strict(wsResponse.bodyAsBytes, None))
  }
}