Todor Kolev
Todor Kolev

Reputation: 1482

How to forward a WSResponse without explicitly mapping the result

So I have the following action and I would like to find a way to directly return the response without having to map it to a result for every possible status code i.e skip the if-else part.

def testAction = Action { implicit requestIn => {

   val requestOut : WSRequest = WS.url("test-domain-name:9998")

   val queryString = requestIn.queryString.map { case (k,v) => k -> v.mkString }

   val futureResponse : Future[WSResponse] = requestOut.withQueryString(queryString.toList: _*).get()

   val response = Await.result(requestOut.withQueryString(queryString.toList: _*).get(), 5 seconds)

   if(response.status == 200) {
      Ok(response.xml)
   } else {
      BadRequest(response.body)
   }

}

Upvotes: 4

Views: 2069

Answers (3)

Kyr
Kyr

Reputation: 5491

This is an updated answer based on Barak BN's and Mon Calamari's answer, for Play 2.6:

import play.api.http.HttpEntity
import play.api.libs.ws.WSResponse
import scala.concurrent.Future

def response2Result(response: Future[WSResponse]): Future[Result] = {
  response map {
    response =>
      val headers = response.headers
        .map { h => (h._1, h._2.head) }
        .filter { _._1.toLowerCase != "content-length" }
      Result(
        ResponseHeader(response.status, headers),
        HttpEntity.Strict(response.bodyAsBytes, Some(response.contentType))
      )
  }
}

The main differences is the replace of allHeaders by headers in 2.6 and removal of "content-length" header from Result because "explicit Content-Length header is not allowed" by Akka.

Also check the discussion in issue #2239 and PR #4787.

Upvotes: 2

Barak BN
Barak BN

Reputation: 558

Variation on Mon Calamari's answer, for Play 2.5

implicit def Response2Result(response: Future[WSResponse]): Future[Result] = {
  response map {
    response =>
      val headers = response.allHeaders map {
        h => (h._1, h._2.head)
      }
      Result(ResponseHeader(response.status, headers), Strict(response.bodyAsBytes, None))
  }
}

Upvotes: 4

Mon Calamari
Mon Calamari

Reputation: 4463

You should not await for result. Play framework supports async actions:

def testAction = Action.async { implicit requestIn =>

    val requestOut: WSRequest = WS.url("test-domain-name:9998")

    val queryString = requestIn.queryString.map { case (k, v) => k -> v.mkString }

    val futureResponse: Future[WSResponse] = requestOut.withQueryString(queryString.toList: _*).get()

    futureResponse

}

And implicit conversion:

implicit def Response2Result(response: Future[WSResponse]): Future[Result] = {
  response map {
    response =>
      val headers = response.allHeaders map {
        h => (h._1, h._2.head)
      }
      Result(ResponseHeader(response.status, headers), Enumerator(response.body.getBytes))
  }
}

See github issue.

Upvotes: 6

Related Questions