Ktor offers an
Attributes class that acts as a small typed instance container/dependency injector.
For the server, the
ApplicationCall contains an
attributes property including an instance of this class (
call.attributes ). The lifespan of this container is the call: beginning when the request has started and ending when the response has been sent.
You can set as many attributes as required per call on an interceptor and retrieve them later in another interceptor.
In the case of the client, the
HttpRequest also contains an
attributes property. So from a
HttpClientCall instance, you can access the attributes with
It is possible to define your own typed attributes by creating instances of the
AttributeKey class like this:
You can later set the attributes with:
And retrieve them in another interceptor by calling:
The full interface for this class looks like: