I'm having apparently a problem with ContentNegoti...
# ktor
s
I'm having apparently a problem with ContentNegotiation in a Ktor 3.1.1, Kotlin 2.1.10 project. So this works:
Copy code
get("/stream/{n}") {
            call.respondBytes(
                contentType = null,
                status = HttpStatusCode.OK,
            ) {
                ByteArrayOutputStream(64).apply {
                    repeat(3) {
                        write("Hello Bytes!".toByteArray())
                    }
                }.toByteArray()
            }
        }
it outputs:
Copy code
curl -X GET "<http://localhost:8081/stream/1>"
Hello Bytes!Hello Bytes!Hello Bytes!
but using
respondBytesWrite
Copy code
get("/stream/{n}") {
            call.respondBytesWriter(
                contentType = null,
                status = HttpStatusCode.OK
            ) {
                repeat(3) {
                    writeByteArray("Hello Bytes!".toByteArray())
                }
            }
        }
leads to an empty response:
Copy code
curl -X GET "<http://localhost:8081/stream/1>"
curl: (52) Empty reply from serve
but when I comment out the ContentNegotiation dependencies
ktor-server-content-negotiation-jvm
and
ktor-serialization-kotlinx-json-jvm
, it works again 🤷‍♂️ My ContentNegotiation config is pretty bare metal:
Copy code
fun Application.configureSerialization() {
    install(ContentNegotiation) {
        json()
    }
}
What am I missing?
1
a
Do you use the Maven build system?
s
yes, i do.
m
if you hint the content-length and content-type, does it work?
e.g.
application/octet-stream
and
36
in this case (3x "Hello Bytes!" length)
a
Most likely, this is KTOR-7662. Can you try to explicitly define the
kotlinx-io-core-jvm
dependency as described in the comment?
s
@madisp just setting the content-length is enough! 🤯 (the content-type makes no difference). but I don't know the content-length at this point, I build it up dynamically and first writing everything to a buffer defeats the purpose of using a write (less copying) 😮
m
give Aleksei's
kotlinx-io-core-jvm
dependency a try, sounds like a bug with chunked transfer encoding due to that
if possible, I recommend trying to figure out the content length though, chunked transfer encoding is a pain in general
s
will do. Paralell to me my collegue just replaced
ktor-serialization-kotlinx-json-jvm
with
ktor-serialization-jackson-jvm
and that fixes it as well.
yes, the
kotlinx-io-core-jvm
dependency fixes it as well! Thanks 👍