2

I'm receiving a file as a Bytestring from Kafka Reactive Streams consumer; I want to construct an akka-http request with this Bytestring as an entity HttpEntity.Default. HttpEntity.Default requires Source[Bytestring, Any] as one of its parameters.

What is a best way to connect the two?

Rabzu
  • 52
  • 5
  • 26

1 Answers1

3

You can use Source.single:

HttpEntity.Default(
  ContentTypes.`application/octet-stream`,
  byteString.size,
  Source.single(byteString)
)

That said, are you really sure you need exactly HttpEntity.Default? You can use the HttpEntity.apply(ContentType, ByteString) method to construct an entity directly out of a ByteString:

HttpEntity(ContentTypes.`application/octet-stream`, byteString)

It returns an instance of HttpEntity.Strict instead of HttpEntity.Default, but Strict can be used for sending HTTP requests just fine.

Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
  • 1
    Thank you, this will definitely work. Is there away I can extract Source[Bytestring] from Kafka consumer client so that I can chain the stream from Kafka to Akka-http: because now I load entire Bytestring in memory and then do an akka-http request? – Rabzu Sep 29 '16 at 09:46
  • I'm sorry, I don't know because I never used Kafka. But I suspect it is possible, at least, I see something very like it [in reactive-kafka](http://doc.akka.io/docs/akka-stream-kafka/current/consumer.html#connecting-producer-and-consumer) docs. You probably should ask another question about it. – Vladimir Matveev Sep 29 '16 at 18:42
  • Im using HttpEntity.Default because the file size is known – Rabzu Sep 29 '16 at 22:31