Hi guys, a naive doubt, please bear with me. Any g...
# server
m
Hi guys, a naive doubt, please bear with me. Any good example with gzipping API responses in Kotlin?
c
Ktor has compression built-in to its core, just needs to be enabled https://ktor.io/servers/features/compression.html
For any other framework, I’m sure there are standard compression mechanisms there as well. It’s not really a concern of the Kotlin language, but the framework you’re using
m
spring-boot
z
There's a #spring channel
f
Spring Boot has compression too. Just a yml/properties configuration.
Copy code
server.compression.enabled: true
I always check built in features viewing this doc: https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
👍 1
m
Copy code
fun gzip(content: String): ByteArray {
        val bos = ByteArrayOutputStream()
        GZIPOutputStream(bos).bufferedWriter(UTF_8).use { it.write(content) }
        return bos.toByteArray()
    }
I used this piece of code. The response times are crazy.
t
Copy code
GZIPOutputStream(res.outputStream, true).use { gzippedStream ->
    content.copyTo(gzippedStream)
}
just write it directly to the client