1
votes

I am trying to implement a proxy that forwards a post request. (Play 2.5.15 Framework and Scala 2.11.11) The request is a multipart call which can contain a huge file, that I can't store in memory nor on disk as a temp file. I found a code snipped in the documentation (https://www.playframework.com/documentation/2.5.x/ScalaBodyParsers#directing-the-body-elsewhere) which would probably do the trick.

import javax.inject._
import play.api.mvc._
import play.api.libs.streams._
import play.api.libs.ws._
import scala.concurrent.ExecutionContext
import akka.util.ByteString

class MyController @Inject() (ws: WSClient)(implicit ec: ExecutionContext) {

 def forward(request: WSRequest): BodyParser[WSResponse] = BodyParser { req =>
    Accumulator.source[ByteString].mapFuture { source =>
      request
        // TODO: stream body when support is implemented
        // .withBody(source)
        .execute()
        .map(Right.apply)
    }
}

def myAction = Action(forward(ws.url("https://example.com"))) { req =>
    Ok("Uploaded")
    }
}

unfortunately the stream body support is not available, yet. Is there a way to achieve it? I couldn't find a suitable solution on the net. I am an absolute scala and play rookie so I am thankful for every hint.

2

2 Answers

1
votes

I also encountered a case in which I found playframeworks's WSClient not right for my needs.

I used akka-http instead. Akka-http is based on akka-streams, and as such will fulfill your streaming (non-buffering) needs. You can get started very quickly using the request-level client API: http://doc.akka.io/docs/akka-http/current/scala/http/client-side/request-level.html#request-level-api

A downside of this approach is that it is not clear to me how to unit test code which use akka-http instead of WSClient.

0
votes

Play 2.6 is released :) the recent release of Play supports forwarding of request bodies. As it is possible for me to update my Problem is solved

https://www.playframework.com/documentation/2.6.x/ScalaBodyParsers#directing-the-body-elsewhere