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

Arnab

11/11/2021, 1:09 PM
Hey there! If I have a ktor client and some sealed class
Foo
that has
Bar
and
Baz
as subclasses, and I make a post request that can either create a
Bar
or a
Baz
, how can I do that? Do I say
<http://myClient.post|myClient.post><Foo> { … }
and then process the entity as either
Bar
or
Baz
?
Copy code
val response = client.request<HttpResponse>("<https://example.com>")
if (response.status == HttpStatusCode.OK) {
    response.receive<SuccessResponse>()
} else {
    response.receive<ErrorResponse>()
}
d

Dennis Schröder

11/18/2021, 11:32 AM
Or use a when statement
Copy code
when(val response = client.request<HttpResponse>("<https://example.com>")) {
 is SuccessResponse -> ... do success stuff
 is ErrorResponse -> ... do error stuff
}
👀 1
2 Views