I use Basic Authentication module with ktor client and Apache HTTP. The authentication module fails to insert Authorization header field due to following code:
The status code responded from Apache creates HttpStatusCode("401","") but it compares with the pre-defined status code, which is HttpStatusCode("401","Unthorized"), and it is structurally not equivalent.
Sorry I'm not sure if should also put a question here, but it's related though..
The way authentication works here from my understand is it tries to call the endpoint once and check for Unauthorized before inserting the Authorization header. This doesn't make sense to me, if a dev configures HTTP client for authentication, it should mean they expect them to happen, shouldn't it? Why should the client makes the double calls to do so?
@e5l Shouldn't the fix be more global?
The class HttpStatusCode represents a status Code. The description is here more to help during debugging in case of an not known status code. Therefore IMHO this class should implement equals only on the code property.
It will save future bugs.
Yep. Probably should. Thanks for the mention, probably we update it with 1.2.0.
I still have the problem, but it happens on every engine tried. Manually settings the header works tho.
Ktor Client 1.1.3 plain JVM, Apache engine.
HttpClient(Apache|Jetty|CIO) { // <-- all 3 engines have the problem
install(Logging) {
level = LogLevel.ALL
}
install(Auth) {
basic {
username = Settings.mainServerUsername
password = Settings.mainServerPassword
}
}
install(JsonFeature) {
serializer = KotlinxSerializer()
}
}
Logging output is:
11:16:45.182 [DefaultDispatcher-worker-1] INFO io.ktor.client.HttpClient - REQUEST: http://localhost:8080/api/newCamera
11:16:45.182 [DefaultDispatcher-worker-1] INFO io.ktor.client.HttpClient - METHOD: HttpMethod(value=POST)
11:16:45.182 [DefaultDispatcher-worker-1] INFO io.ktor.client.HttpClient - HEADERS
11:16:45.182 [DefaultDispatcher-worker-1] INFO io.ktor.client.HttpClient - -> Accept: application/json
11:16:45.183 [DefaultDispatcher-worker-1] INFO io.ktor.client.HttpClient - BODY Content-Type: application/json
11:16:45.184 [DefaultDispatcher-worker-1] INFO io.ktor.client.HttpClient - BODY START
11:16:45.184 [DefaultDispatcher-worker-1] INFO io.ktor.client.HttpClient - "TEST"
11:16:45.184 [DefaultDispatcher-worker-1] INFO io.ktor.client.HttpClient - BODY END
11:16:45.290 [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - RESPONSE: 401
11:16:45.291 [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - METHOD: HttpMethod(value=POST)
11:16:45.291 [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - FROM: http://localhost:8080/api/newCamera
11:16:45.292 [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - HEADERS
11:16:45.292 [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - -> WWW-Authenticate: Basic realm=local-server, charset=UTF-8
11:16:45.292 [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - -> Connection: keep-alive
11:16:45.292 [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - -> Content-Length: 0
11:16:45.292 [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - -> Date: Tue, 19 Mar 2019 10:16:45 GMT
11:16:45.293 [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - BODY Content-Type: null
11:16:45.293 [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - BODY START
Having serialization problem as well.
Could you file a separate issue?
For the folks arriving here and wondering how to fix it, it was visibly fixed in later versions, but you eventually need to add the "sendWithoutRequest = true" in your basic auth configuration (e.g. : https://api.ktor.io/1.3.1/io.ktor.client.features.auth.providers/-basic-auth-provider/)