Auth

Estimated reading time: 1 minute

Ktor client supports authentication out of the box as a standard pluggable feature:

This feature is defined in the class io.ktor.client.features.auth.Auth in the artifact io.ktor:ktor-client-auth:$ktor_version.
dependencies { implementation "io.ktor:ktor-client-auth:$ktor_version" }
dependencies { implementation("io.ktor:ktor-client-auth:$ktor_version") }
<project> ... <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-auth</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> </dependencies> </project>

Installation

val client = HttpClient() {
    install(Auth) {
        // providers config
        ...
    }
}

Providers

Basic

This provider sends an Authorization: Basic with the specified credentials:

val client = HttpClient() {
    install(Auth) {
        basic {
            username = "username"
            password = "password"
        }
    }
}

This feature implements the IETF’s RFC 7617.

Digest

This provider sends an Authorization: Digest with the specified credentials:

val client = HttpClient() {
    install(Auth) {
        digest {
            username = "username"
            password = "password"
            realm = "custom"
        }
    }
}

This feature implements the IETF’s RFC 2617.