sorry for the repeat, but still actual :blush:
# ktor
e
sorry for the repeat, but still actual 😊
📝 1
d
Copy code
val MyAttributeKey = AttributeKey<Int>("MyAttributeKey")
call.attributes.put(MyAttributeKey, 10)
call.attributes.get(MyAttributeKey)
The
ApplicationCall
contains a
attributes
property that acts as a small typed dependency injector/instance container bounded to the call (so its lifespan is between the request until the response is completed). You can put any attribute you want in an interceptor that executes before, and then receive it later in another interceptor
I have updated the documentation to explain this in detail: https://ktor.io/advanced/pipeline/attributes.html Let me know if it works for you
e
Isn’t
HttpClientCall
used for ktor client?
I found only
HttpRequestBuilder.setAttributes
, but not sure if it works at all
something like this
Copy code
install("MyFeature") {
    sendPipeline.intercept(HttpSendPipeline.Before) {
        this.context.setAttributes { 
            put(key, 1)
        }
    }
}
d
sorry, didn’t read the client part. I guess the client also has an
attributes
property, but let me check
yes, I think that’s what you have to use
the
HttpRequest
interface also has an attributes
So maybe
call.request.attributes
?
e
not sure how can i get it inside
sendPipeline
or
requestPipeline
d
let me check
e
PipelineContext<Any, RequestBuilder>
is used inside interceptors so i can use
HttpRequestBuilder
but don’t see any way to get to the
HttpRequest
it works inside
receivePipeline
tho
ok, let me explain what I want to do with this, may be I’m digging in the wrong way
d
@e5l
it looks that you cannot access an attributes directly there, not sure if it has an specific reason, so let’s wait for a response
e
I want to pass extra info on call, like method name. Also I want to record time that was spent during request, I do this by wrapping
process()
with before/after timestamps. And in the end log all this data with extra info, passed in the beginning and total time
e
Could you use
HttpRequestBuilder.setAttribute
to mark the request? You could get request from the response using
call
field.
e
I tried to use
setAttribute
but it was never called how can i get
call
field inside
requestPipeline
?
Copy code
receivePipeline.intercept(HttpReceivePipeline.Before) {
                println(context.request.attributes.takeOrNull(key))
            }
I also tried this, but it’s still empty and
setAttribute
was not called
e
Oh, I see. It's obvious a bug. I'll file an issue and fix it asap.
👍 1
e
Thanks!