Search code examples
scalaakka-http

Akka-http - write to response output stream


I need to create a large tab separated file as a response to HTTP GET request. In my route a create some Scala objects and then I want to write some custom representation of those objects to the Output Stream.

It is not just serialization to tab separated instead of JSON, because I need also to create a header with column names, so IMHO this can't be solved with custom marshaling.

So how can I get a writer or outputstream from HttpRequest?

Something like

 ~path("export") {
        get {
              val sampleExonRPKMs = exonRPKMService.getRPKMs(samples)
              val writer = HttpResponse().getWriter // this does not exists
              writeHeader(writer)
              ... // write objects tab separated
        }
   }

Solution

  • You can complete an Akka HTTP route with a marshallable source. If you don't want to use custom marshallers, you can always complete with a Source[ByteString, _]. See the docs for more info.

    Your route will look something like

    get {
      val sampleExonRPKMs = exonRPKMService.getRPKMs(samples)
      val headers: String = ???
      Source.single(headers).concat(Source(sampleExonRPKMs).map(_.toTSVLine)).intersperse("\n").map(ByteString.apply)
    }
    

    Note as a separate issue: if you are dealing with large amounts of data, the getRPKMs call will result in loading all of it in memory.