0

I want to stream large responses from remote servers.

For example - I used the following code to stream an image from wikimedia. The expectation was that it will stream the image back to the client in small chunks. However, it loads the entire image and then sends it to the client.

Action.async { implicit request =>
    val futureResponse = ws.url(
      "https://upload.wikimedia.org/wikipedia/commons/c/c7/%2271%22_on_St._Patrick%27s_Day._Washington%2C_D.C.%2C_March_17._" +
        "Justice_Pierce_Butler%2C_71_years_old_today_celebrates_his_birthday_by_taking_his_morning_walk%2C_snapped_while_leaving_his_home_on_19" +
        "th_LCCN2016871374.tif")
      .withMethod("GET")
      .stream()
    futureResponse.map {
      f =>
        Ok.chunked(f.bodyAsSource)
    }
  } 

I copied the entire code given in https://www.playframework.com/documentation/2.8.x/ScalaWS#Processing-large-responses but it too does not work

Play version is 2.8.19.

Gaël J
  • 11,274
  • 4
  • 17
  • 32
mohit
  • 4,968
  • 1
  • 22
  • 39
  • What makes you say it does not work? Pretty sure I'm using similar code in some of my apps and it works fine. Does Wikimedia support download in streaming in the 1st place? – Gaël J May 19 '23 at 11:46
  • @Gaël When I do a curl, the progress does not change till the entire file is downloaded and then suddenly the progress on curl becomes 100 percent. Hitting wikimedia directly, the progress gradually increases. I changed the code from WS to sttp which made it work. The curl shows gradual progress. – mohit May 19 '23 at 12:50
  • 1
    I'd guess that this is because the code doesn't signal what the content-length is in the response, so curl has no way of measuring progress. – Michael Zajac May 19 '23 at 16:13
  • @MichaelZajac - I tried with sttp instead of WS and it works fine. Except the library everything is same and curl picks it properly. My colleague tried with Akka http and it was fine with Akka http too. With WS, the code does not go into the futureResponse block till the file is downloaded and as soon as it enters the response is sent. – mohit May 19 '23 at 17:37

0 Answers0