I am trying to write a plugin that cancels the cal...
# ktor
d
I am trying to write a plugin that cancels the call at the 302 and 401 status and instead executes a configured lambda from the installation. However, the response status is not set in the HttpResponsePipeline phases. How can I determine this? Or is my code already wrong or a wrong approach?
Copy code
override fun install(plugin: LoginPlugin, scope: HttpClient) {
            <http://console.info|console.info>("Installiere Plugin")

            scope.responsePipeline.intercept(HttpResponsePipeline.Receive) { (info, body) ->
                val status = context.response.status // Always 0
                <http://console.info|console.info>("Ausführung mit Status $status")
                if (!(status == HttpStatusCode.Unauthorized || status == HttpStatusCode.TemporaryRedirect || status == HttpStatusCode.Found)) return@intercept

                log.debug { "Aufruf des lambdas und Weiterverarbeitung" }
                plugin.loginLambda(context.response)
                //cancel Response
                finish()
            }
Same Status with HttpSend:
a
Could you please explain what do you mean by cancelling a call? Here is an example of cancelling the call with a
CancellationException
:
Copy code
val client = HttpClient()

client.plugin(HttpSend).intercept { builder ->
    val call = execute(builder)

    if (call.response.status == HttpStatusCode.Found) {
        builder.executionContext.cancel(CancellationException("Status ${call.response.status} is returned from the server"))
    }

    call
}

try {
    client.get("<https://httpbin.org/status/302>")
} catch (e: CancellationException) {

}
d
Hello @Aleksei Tirman [JB], sorry for the late reply. I fell ill directly after asking the question and am only back in the office now. The plan is to react to the status Unauthorized in the (web) client for all requests and to offer a corresponding note with a link to the login. However, this should not be added manually for each Api request. Therefore, I thought that I could possibly realise this via a plugin. Is this the wrong way to go?