Hi all :slightly_smiling_face: I am trying to make...
# ktor
j
Hi all 🙂 I am trying to make a
ClientPlugin
to replace an OkHttp
Interceptor
in Android that implements the headers described in the DPoP spec. It all seems very straightforward, but I can't seem to get the raw request body to add to my DPoP token. The body I get from
onRequest
appears to be the data class, and I can see that I have the
bodyType
as well but I cannot figure out how to use this to get a serialized version of the request body. I feel like I am missing something really obvious or I am doing something fundamentally wrong. I am using Kotlin Serialization and the OkHttp engine, but I really want to convert my OkHttp interceptors so I can leverage coroutines and get the headers into the ktor logging plugin.
I found
SendingRequest
, which let me get things a little later. Not entirely sure this is a good idea but it works!
Copy code
on(SendingRequest) { request, content ->
    val bodyContent = (content as TextContent).text
    val dpop = makeDPoPTokenForKtor(
        requestBody = bodyContent,
        httpMethod = request.method.value,
        url = request.url.build(),
        accessToken = keyHandler.accessToken,
        currentTime = clockSync.now(),
    )
    val signedDPoPToken = signDPoPToken(
        dPoPToken = dpop,
        privateKey = keyHandler.privateKey,
        publicKey = keyHandler.publicKey,
    )
    request.headers.append("DPoP", signedDPoPToken)
}
a
You can add a handler for the
Send
hook where the raw request body is available. Here is an example:
Copy code
val client = HttpClient(CIO) {
    install(createClientPlugin("plugin") {
        on(Send) { request ->
            val bodyBytes = (request.body as OutgoingContent).toByteArray()
            request.header("custom", bodyBytes.size.toString())
            proceed(request)
        }
    })
}