Alexander Städing
03/29/2024, 10:40 AMclient.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())
}
}
Aleksei Tirman [JB]
03/29/2024, 10:45 AMAlexander Städing
03/29/2024, 10:45 AMAleksei Tirman [JB]
03/29/2024, 10:46 AMAlexander Städing
03/29/2024, 10:54 AMAlexander Städing
03/29/2024, 10:55 AMAlexander Städing
03/29/2024, 11:18 AMsetBody
method with the protobuf object. If I use setBody("foo")
the protobuf content type header is sent.Alexander Städing
03/29/2024, 11:20 AMwriteTo
method is called:
override suspend fun serializeNullable(contentType: ContentType, charset: Charset, typeInfo: TypeInfo, value: Any?): OutgoingContent? {
if (value !is GeneratedMessage) return null
return object : OutgoingContent.WriteChannelContent() {
override suspend fun writeTo(channel: ByteWriteChannel) = value.writeTo(channel.toOutputStream())
}
}
Aleksei Tirman [JB]
03/29/2024, 11:31 AMProtobufContentConverter
?Alexander Städing
03/29/2024, 11:33 AMserializeNullable
incorrectly, and that I have to somehow write the ContentType again. Here is the full code:
https://gist.github.com/alexstaeding/27b7bc51d8ed4edcb1170d4016e4e50fAlexander Städing
03/29/2024, 11:34 AMAleksei Tirman [JB]
03/29/2024, 11:34 AMcontentType
property of the OutgoingContent.WriteChannelContent
object in the serializeNullable
method.Alexander Städing
03/29/2024, 11:36 AM