https://kotlinlang.org logo
#ktor
Title
# ktor
s

steamstreet

07/13/2022, 6:34 PM
I think I’ve discovered a bug with gzip client handling (and likely all compression plugins). If I issue a
get
for compressed data at a URL, and then call
response.body<ByteArray>()
the response bytes are truncated to the content length of the response, but that value is the compressed size. So it’s taking the uncompressed bytes, and truncated them to the size of the compressed data. Is there another way to get at the uncompressed data without doing the gzip stuff myself?
a

Aleksei Tirman [JB]

07/14/2022, 8:08 AM
Could you please share a code snippet to illustrate this problem?
s

steamstreet

07/17/2022, 7:30 PM
Copy code
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.java.*
import io.ktor.client.plugins.compression.*
import io.ktor.client.request.*
import io.ktor.http.*

suspend fun downloadBytes() {
    val client = HttpClient(Java) {
        install(ContentEncoding) {
            gzip()
        }
    }

    val response = client.get("<https://www.knkx.org/podcast/forgotten-prison/rss.xml>")

    val contentLength = response.contentLength()
    println("Content length: ${contentLength}")

    val responseData = response.body<ByteArray>()
    println("Data length: ${responseData.size}")
}
You’ll see that contentLength and responseData.size are both the same value (3145 when I ran it). But if you use
response.body<String>()
you get a string that is 14622 characters long. I know I can call the string version, but there are other places where I need to get at the uncompressed bytes.
a

Aleksei Tirman [JB]

07/18/2022, 6:36 AM
Thank you. I’ll file an issue soon. As a workaround, you can receive a byte array with the following code:
Copy code
val responseData = response.bodyAsChannel().readRemaining().readBytes()
17 Views