How does ktor serialize `body`, which is of type `...
# ktor
s
How does ktor serialize
body
, which is of type
Any
? When I do something like this, it fails..
Copy code
@Serializable
class Foo(val bar: String)

@Test
fun test() {
    val any: Any = Foo("asd")
    Json.encodeToString(any).also { println(it) }
}
a
Could you please share a code with Ktor involved?
s
Copy code
private suspend inline fun <reified V> MethodData.call(): V {
        return <http://httpClient.post|httpClient.post> {
            url(endpoint(token, id))
            contentType(ContentType.Application.Json)
            body = this@call
        }
    }
above has
body
type of
Any
MethodData
is interface. I call
call
method on subclasses of
MethodData
but in the scope of
call
it is denoted by
MethodData
. I believe same thing is happening in my above code. But it is failing for my code, where as ktor is able to encode the body.
a
There is a method HttpClient.defaultTransformers that creates an instance of
OutgoingContent
object depending on a body's actual type.
s
Won’t it evaluate to
content = null
since
ContentType
is neither
Plain/ByteArray/ByteReadChannel
?
a
If you have the
JsonFeature
installed then it will return TextContent object earlier (a serializer will be determined on this line).
s
Ah I see. Now I get it. Thanks a lot!