https://kotlinlang.org logo
#ktor
Title
# ktor
e

einar

03/20/2019, 8:42 PM
Is there a way to, using the ktor client, handle a response that can be of different type when performing a POST request? I was thinking of using
sealed class
for this but I’m falling short when it comes to supporting the de-serialization of the response. So something like
sealed class ApiResponse
<http://client.post|client.post><ApiResponse> {  }
@Serializable data class ErrorResponse(...) : ApiResponse()
@Serializable data class SuccessResponse(..) : ApiResponse()
r

r4zzz4k

03/20/2019, 8:50 PM
Do you use
KotlinxSerializer
? Is it JVM or some other target?
e

einar

03/20/2019, 8:52 PM
Yes, I’m using `KotlinSerializer`and targeting iOS and Android in MPP
r

r4zzz4k

03/20/2019, 9:00 PM
iOS can't acquire serializers via reflection, so you'll have to register all your serializers manually. Also, as far as I'm aware, polymorphic serializer for multi-platform is WIP right now.
https://github.com/Kotlin/kotlinx.serialization/pull/394 Here's PR which you can track for progress. Unfortunately I'm unable to find relevant issue.
e

einar

03/20/2019, 9:16 PM
Excellent, thank you for pointing me towards this 👍
r

r4zzz4k

03/20/2019, 9:18 PM
Glad I could help!
4 Views