https://kotlinlang.org logo
#ktor
Title
# ktor
f

fkrauthan

02/16/2022, 12:26 AM
If I have a
ktor-client
feature that has a
sendPipeline
and a
receivePipeline
intercept. How can I pass values from the sendPipeline intercept to the receivePipeline intercept (e.g. a class instance and/or some value)?
a

Aleksei Tirman [JB]

02/16/2022, 6:32 AM
You can use Attributes (the documentation is for the server but the client uses the same interface) to pass a value among pipelines:
Copy code
val keyForObject = AttributeKey<MyData>("")
client.sendPipeline.intercept(HttpSendPipeline.Before) {
    context.attributes.put(keyForObject, MyData())
}

client.receivePipeline.intercept(HttpReceivePipeline.State) {
    println(context.attributes[keyForObject])
}
f

fkrauthan

02/16/2022, 5:50 PM
Ah cool perfect. And anything can be an attribute? Including class instances?
a

Aleksei Tirman [JB]

02/16/2022, 5:59 PM
Yes.
f

fkrauthan

02/16/2022, 6:35 PM
Cool thank you so much. I'll give it a try
9 Views