Auth

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.

  1. dependencies {
  2. implementation "io.ktor:ktor-client-auth:$ktor_version"
  3. }
  1. dependencies {
  2. implementation("io.ktor:ktor-client-auth:$ktor_version")
  3. }
  1. <project>
  2. ...
  3. <dependencies>
  4. <dependency>
  5. <groupId>io.ktor</groupId>
  6. <artifactId>ktor-client-auth</artifactId>
  7. <version>${ktor.version}</version>
  8. <scope>compile</scope>
  9. </dependency>
  10. </dependencies>
  11. </project>
  1. dependencies {
  2. implementation "io.ktor:ktor-client-auth-jvm:$ktor_version"
  3. }
  1. dependencies {
  2. implementation("io.ktor:ktor-client-auth-jvm:$ktor_version")
  3. }
  1. <project>
  2. ...
  3. <dependencies>
  4. <dependency>
  5. <groupId>io.ktor</groupId>
  6. <artifactId>ktor-client-auth-jvm</artifactId>
  7. <version>${ktor.version}</version>
  8. <scope>compile</scope>
  9. </dependency>
  10. </dependencies>
  11. </project>
  1. dependencies {
  2. implementation "io.ktor:ktor-client-auth-native:$ktor_version"
  3. }
  1. dependencies {
  2. implementation("io.ktor:ktor-client-auth-native:$ktor_version")
  3. }
  1. <project>
  2. ...
  3. <dependencies>
  4. <dependency>
  5. <groupId>io.ktor</groupId>
  6. <artifactId>ktor-client-auth-native</artifactId>
  7. <version>${ktor.version}</version>
  8. <scope>compile</scope>
  9. </dependency>
  10. </dependencies>
  11. </project>
  1. dependencies {
  2. implementation "io.ktor:ktor-client-auth-js:$ktor_version"
  3. }
  1. dependencies {
  2. implementation("io.ktor:ktor-client-auth-js:$ktor_version")
  3. }
  1. <project>
  2. ...
  3. <dependencies>
  4. <dependency>
  5. <groupId>io.ktor</groupId>
  6. <artifactId>ktor-client-auth-js</artifactId>
  7. <version>${ktor.version}</version>
  8. <scope>compile</scope>
  9. </dependency>
  10. </dependencies>
  11. </project>

Installation

  1. val client = HttpClient() {
  2. install(Auth) {
  3. // providers config
  4. ...
  5. }
  6. }

Providers

Basic

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

  1. val client = HttpClient() {
  2. install(Auth) {
  3. basic {
  4. username = "username"
  5. password = "password"
  6. }
  7. }
  8. }

This feature implements the IETF’s RFC 7617.

Digest

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

  1. val client = HttpClient() {
  2. install(Auth) {
  3. digest {
  4. username = "username"
  5. password = "password"
  6. realm = "custom"
  7. }
  8. }
  9. }

This feature implements the IETF’s RFC 2617.