Hey guys, I'm struggling with a weird issue in ktor using protobuf content negotiation. It seems that the application/protobuf Content-Type header is being set on the client but not received on the server. When I set the Content-Type header to something else (e.g. text) it works. Any ideas? Here is the code:
client.post("<http://localhost:8080/launch>") {
contentType(ContentType.Application.ProtoBuf)
setBody(launch)
println("Sent content type: ${contentType()}")
}
This prints:
Sent content type: application/protobuf
Response: HttpResponse[<http://localhost:8080/launch>, 415 Unsupported Media Type]
But the server receives no Content-Type header:
[24-Mar-29 11:37:47] [INFO] - Autoreload is disabled because the development mode is off.
[24-Mar-29 11:37:47] [INFO] - Application started in 0.034 seconds.
[24-Mar-29 11:37:47] [INFO] - Responding at <http://0.0.0.0:8080>
[24-Mar-29 11:37:53] [WARN] - Content-Type: null
val logger by inject<Logger>()
post("/launch") {
val contentType = call.request.header(HttpHeaders.ContentType)
logger.warn("Content-Type: $contentType")
If I use,
contentType(ContentType.Text.Plain
, the server prints:
[24-Mar-29 11:39:24] [WARN] - Content-Type: text/plain; charset=UTF-8
P.S. I am using
.proto
files which are converted to Java with the protobuf gradle plugin. I wrote my own
ContentConverter
and register it like this:
val client = HttpClient {
install(ContentNegotiation) {
register(ContentType.Application.ProtoBuf, ProtobufContentConverter())
}
}