Tyler
Tyler

Reputation: 18187

Sending an already Gzip'd payload to the client

I am struggling to send a response that is a Gzip file from S3. I am downloading the file from S3 as a Array[Byte] and then I would like to pass that along to the client and have the browser take care of the unzipping.

To be clear, I am not using the built-in gzipFilter in Play, because the data is already zipped.

I have this in my controller's action:

s3DAO.getFile(id).map(bytes => Ok(bytes).as("application/gzip”))

On the client (using Postman to debug), the Content-Type header is set to application/gzip, but the body comes out looking like this:

���[�W�$��j3��-��/�-HOz��ƒ@�HȄ%D���E�Л��%�=P�����$�*v7��X����������=޾x|��w������챕8�,��X���

I know that the bytes represent a valid payload, because if I insert code for decompressing it myself in the controller action, I can println it out fine:

val zipInputStream = new GZIPInputStream(new ByteArrayInputStream(bytes))
val rawString = scala.io.Source.fromInputStream(zipInputStream, Charsets.UTF_8.name).mkString
println(rawString) // I can see my payload

I do not know what to do to debug this any further

Upvotes: 2

Views: 99

Answers (1)

Tyler
Tyler

Reputation: 18187

I posted the same question on the Play Gitter Channel and found out the answer was to set the Content-Type to octet-stream and then to specify the encoding as gzip:

Ok(bytes).as("application/octet-stream").withHeaders("Content-Encoding" -> "gzip")

Upvotes: 3

Related Questions