0

I have a Ktor client requesting from a server with Basic Auth.

When I install() the Basic Auth as per the docs, it does not work - I get back HTTP 401.

val response1 =
    HttpClient(Java.create()) {
        install(Auth) {
            basic {
                credentials {
                    BasicAuthCredentials("foo@example.com", pass)
                }
            }
        }
    }
   .use { it.get("https://mycompany.something.net/rest/servicedeskapi/request") }

I need to add the Auth to the request to make it work:

      .use { 
          it.get("https://mycompany.something.net/rest/servicedeskapi/request") {
              basicAuth("foo@example.com", pass)
          }
      }

Why is not the auth set up in the client enough? Am I doing something wrongly?

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277

1 Answers1

0

Shortly after posting, I found out that it is de facto a duplicate of Ktor client Auth feature does not sending Authorization header

So, in short: For some reason, by default, Ktor opts not to send the Authorization header until it fails authorization. Which is not too fortunate decision IMHO.

This can be changed by sendWithoutRequest { true }:

    install(Auth) {
        basic {
            sendWithoutRequest { true }
            credentials { BasicAuthCredentials(user, pass) }
        }
    }
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277