Rabzu Rabzu - 2 months ago 22
Scala Question

How to transform Bytestring to Source[Bytestring, Any]

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?

Answer

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.

Comments