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

Lukasz Kalnik

02/29/2024, 10:03 AM
Is there a simple way to accept both success (2xx) and redirects (3xx) in
HttpClient
, but throw on 4xx and 5xx?
expectSuccess
throws on redirects (3xx) as well.
a

Aleksei Tirman [JB]

02/29/2024, 11:00 AM
You can add your custom response validator. Here is an example:
Copy code
val client = HttpClient(CIO) {
    HttpResponseValidator {
        validateResponse { response ->
            if (response.status.value >= 400) {
                throw RuntimeException("Invalid response with status ${response.status}")
            }
        }
    }

}

client.get("<http://httpbin.org/status/404>")
l

Lukasz Kalnik

02/29/2024, 11:12 AM
Thank you very much!