What would be the best way to decode gzipped reque...
# ktor
s
What would be the best way to decode gzipped requests in a ktor server?
a
You can use
GZIPInputStream
to decode incoming request body:
Copy code
post("/") {
    val encoding = (call.request.headers[HttpHeaders.ContentEncoding] ?: "").trim().lowercase()

    if (encoding == "gzip") {
        val channel = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
            GZIPInputStream(call.receiveStream()).toByteReadChannel()
        }

        call.respond(object : OutgoingContent.ReadChannelContent() {
            override fun readFrom(): ByteReadChannel = channel
        })
    }
}
Also, there is a feature request to support it out of the box.
s
thanks!