3

I wanted to use Alpakka for handling S3 upload and download with Akka Steams. However, I got stuck with using Source produced by S3Client within Akka Http routes. The error message I get is:

[error]  found   : akka.stream.scaladsl.Source[akka.util.ByteString,_$1] where type _$1
[error]  required: akka.http.scaladsl.marshalling.ToResponseMarshallable
[error]     complete(source)

I assume that it is some annoying trivial thing, like missing implicit import, but I was not able to pinpoint what I am missing.

I've created some minimal example to illustrate the issue:

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Source
import akka.util.ByteString

import scala.concurrent.ExecutionContext

class Test {

  implicit val actorSystem: ActorSystem = ActorSystem()
  implicit val materializer: ActorMaterializer = ActorMaterializer()
  implicit val executionContext: ExecutionContext = actorSystem.dispatcher

  val route = (path("test") & get) {
    def source: Source[ByteString, _] = ??? // just assume that I am able to get that value
    complete(source) // here error happens
  }

  Http().bindAndHandle(route, "localhost", 8000)
}

Do you have some suggestions, what can I try? I am using

libraryDependencies += "com.typesafe.akka"%% "akka-http" % "10.0.5"
Mateusz Kubuszok
  • 24,995
  • 4
  • 42
  • 64

1 Answers1

8

You need to create an HttpEntity from the source, and give it a content-type.

complete(HttpEntity(ContentTypes.`application/json`, source))
Stefano Bonetti
  • 8,973
  • 1
  • 25
  • 44
  • More specifically, `HttpEntity.Default`. "It has a known length and presents its data as a Source[ByteString]". – Abhijit Sarkar Jul 04 '17 at 20:28
  • 1
    Thanks! I was confused by second paragraph of http://doc.akka.io/docs/akka-http/10.0.9/scala/http/routing-dsl/source-streaming-support.html . I (mis)understood that there is some implicit conversion that I am missing. – Mateusz Kubuszok Jul 04 '17 at 20:32
  • I see. Further implicits are only necessary if you want to stream custom classes directly, converting them to JSON under the hood. – Stefano Bonetti Jul 04 '17 at 20:39