Search code examples
kotlinhttpclientktor

How to disable redirections in ktor-client


I am creating simple HTTP request with ktor-client (ApacheHttpClient engine)

val client = HttpClient(Apache) {
    engine {
        followRedirects = false
        [email protected] = false
    }
}

and using it to submit a form

client.submitForm<HttpResponse>(
        url = "https://foo.com/login",
        formParameters = Parameters.build {
            append("_username", username)
            append("_password", password)
        })

In logs, I can see a correct response with 302-redirection which I want to get and obtain a cookie from it. But instead, I see that the client moves on and makes several more requests and finally fails with:

io.ktor.client.features.SendCountExceedException: Max send count 20 exceeded

How can I completely disable 302-based redirections in ktor-client?


Solution

  • ktor-client follows redirects by default, to prevent infinite redirects use:

    val client = HttpClient(HttpClientEngine) {
        followRedirects = false
    }