eirikb
09/01/2022, 12:34 PMTransfer-Encoding: chunked
. Are there any ways to prevent this? The server we communicate with doesn't support iteirikb
09/01/2022, 12:43 PMsuspend fun main() {
val client = HttpClient(OkHttp) {
install(ContentNegotiation) {
jackson { }
}
}
client.put("<http://localhost:1337/data>") {
contentType(ContentType.Application.Json)
setBody("")
}
}
If I remove jackson
it will not be chunkedeirikb
09/01/2022, 12:48 PMeirikb
09/02/2022, 7:40 AMAleksei Tirman [JB]
09/02/2022, 7:47 AMContentConverter
that will serialize objects to OutgoingContent
with known content length. Here is an example:
class MyJacksonConverter(private val objectmapper: ObjectMapper = jacksonObjectMapper()): ContentConverter {
private val originalConverter = JacksonConverter(objectmapper)
override suspend fun deserialize(charset: Charset, typeInfo: TypeInfo, content: ByteReadChannel): Any? {
return originalConverter.deserialize(charset, typeInfo, content)
}
override suspend fun serializeNullable(
contentType: ContentType,
charset: Charset,
typeInfo: TypeInfo,
value: Any?
): OutgoingContent {
val writer = StringWriter()
objectmapper.writeValue(writer, value)
return TextContent(writer.toString(), contentType)
}
}
Here is an example usage:
suspend fun main() {
val client = HttpClient(OkHttp) {
install(ContentNegotiation) {
register(ContentType.Application.Json, MyJacksonConverter())
}
}
val r = <http://client.post|client.post>("<https://httpbin.org/post>") {
contentType(ContentType.Application.Json)
setBody(Example(123))
}
println(r.bodyAsText())
}
data class Example(val x: Int)
eirikb
09/02/2022, 7:49 AM""
is chunked, but on 2.1.0 it is never chunkedAleksei Tirman [JB]
09/02/2022, 10:28 AMContentNegotiation
plugin.Aleksei Tirman [JB]
09/02/2022, 10:30 AMeirikb
09/02/2022, 10:32 AMgson
thougheirikb
09/02/2022, 10:38 AMconfigure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
and I don't know where to put this setting. With the data I use it will fail without that config. gson
seems to do this by default. I'm not sure if we could just swap to gsonAleksei Tirman [JB]
09/02/2022, 10:48 AMMyJacksonConverter
:
val mapper = ObjectMapper()
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
val client = HttpClient(OkHttp) {
install(ContentNegotiation) {
register(ContentType.Application.Json, MyJacksonConverter(mapper))
}
}
eirikb
09/02/2022, 10:50 AM