Hi guys, I am using Ktor Http client to call REST ...
# ktor
l
Hi guys, I am using Ktor Http client to call REST APIs and I use JsonFeature to deserialize the responses. In case of a response with a status ≠200 OK, my json body cannot be mapped to the same object as when the response is OK. How would you do serializing in this case ? I would have liked something like
client.get<Either<ResponseObject, ErrorObject>>() {}
c
It is possible. First of all you need to disable exception on non-200 responses: https://ktor.io/clients/http-client/features/expect-success.html
e
Hi, @legzo. You could use
Copy code
val response = client.get<HttpResponse>(...)
then check for the response status and then call
Copy code
response.receive<DataClassOfMyChoice>()
👆 1
l
Humm I see. Were you about to suggest the same approach @cy ?
c
Yes
l
Alright, perfect. I'll have a look. Thanks again guys, it's really helpful to have such quick answers
c
I was just delayed with "the second" because my colleague asked me a question, sorry