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

Fabrizio Scarponi

02/17/2022, 2:35 PM
Hi there, I'm here to step up my kotlin specialization! I have a lot of questions to post! but for now, I'm looking for a good example of to how use properly ktor client response validation... can someone post me something? Thanks!
a

Aleksei Tirman [JB]

02/17/2022, 2:39 PM
Here is the documentation.
f

Fabrizio Scarponi

02/17/2022, 6:59 PM
Is not my first time on documentation, spent about 1 day looking for a solution... but when I call receive in the validator, i can't read it another time... the validator consume the stream.. and i don't know how to avoid that.. (in detail some time this backend give me a 200, with an error message inside, so i need to read the response in validator and throw if is a real error, or nothing otherwise....
l

Landon Patmore

02/17/2022, 8:51 PM
Save the value and read the data from the value you saved previously.
a

Aleksei Tirman [JB]

02/18/2022, 8:11 AM
You can make a copy of the request, response, and call objects by executing the
save
method on the
HttpClientCall
instance. Here is an example:
Copy code
val client = HttpClient(Apache) {
    HttpResponseValidator {
        validateResponse { response ->
            val originCall = response.call
            val newCall = originCall.save()
            val text = newCall.response.readText()
            println(text)
        }
    }
}

client.get<String>("<http://example.com>")
f

Fabrizio Scarponi

02/22/2022, 8:37 AM
Here again, sorry but as Boromir teaches Is Not That simple (problem is that I'm not that skilled).. here is the snippet, and the log after first HTTP response is received, and finally the request... it looks like I'm missing to understand something
image.png
Any ideas?
a

Aleksei Tirman [JB]

02/28/2022, 8:56 AM
For some reason, Gson converted a JSON to the
null
.