Hey everyone, how can I copy an `HttpRequest`? I ...
# server
t
Hey everyone, how can I copy an
HttpRequest
? I want to send a new request in
responsePipeline
based on the previous request, with a small difference in the body
🧵 1
My idea is to do something like this:
Copy code
override fun install(feature: TokenAutoRefresh, scope: HttpClient) {
    scope.responsePipeline.intercept(HttpRequestPipeline.State) { (type, content) ->
        if (content !is ByteReadChannel) return@intercept

        with(feature) {
            if (context.response.isTokenValid()) {
                proceed()
            } else {
                val newToken = runCatching { refreshToken?.invoke() }.getOrNull() ?: return@intercept
                val newRequest = context.request.copy() <-- doesn't exist
scope.request(newRequest)
            }
        }
    }
}
r
HttpRequestBuilder().takeFrom(context.request)
🙌 1
is it what you need?
t
Yes it is! Thank you very much for the fast response
👍 1
Do you think this will work? I can’t find any examples online…
r
you can check examples in
Auth
feature. It does very similar to what you try to achieve
👍 1
t
I’m SO close (i think)! I was able to make the interceptor check from the response if the token is valid, and if it isn’t, get a new token. But now I don’t understand how to resend the request with the new token… I tried this:
Copy code
val newRequest = HttpRequestBuilder().takeFrom(context.request).apply {
    header("token", newToken)
}
proceedWith(HttpResponseContainer(type, scope.request(newRequest)))
But unfortunately, it doesn’t seem to do anything and shows no logs…