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

hdarritchon

01/24/2020, 2:41 PM
Hi, I’d like to custom my Ktor response pipeline to add 2 headers, one for the time it takes for the app to process the request and send the response and another one to handle etag response header transparently. I tried to create a Feature, try some interceptors, ... but I failed to find a phase where the response is processed but I am still able to add headers to the response. Often I get this error :
java.lang.UnsupportedOperationException: Headers can no longer be set because response was already completed
. I suppose it’s too late. So I was wandering if it is possible to have a phase where the Request is processed, the response is ready but I’m still able to modify the response to add some headers.
r

ribesg

01/24/2020, 3:35 PM
You could find the phase which is responsible for actually sending the request, then add your own just before it and do your thing on your own added phase
c

cy

01/24/2020, 6:26 PM
One usually should add headers to a response object, not to response itself
h

hdarritchon

01/27/2020, 2:57 PM
After some work, I found a solution for my problem. I used a Feature to register my code to Ktor. In this Feature, I registered 2 phases : • pipeline.intercept(ApplicationCallPipeline.Setup) • pipeline.sendPipeline.intercept(ApplicationSendPipeline.After) In the first phase, I take the start time. In the second phase, I take the stop time and compute the elapse time between start and stop and I add it to the Headers using the method `
Copy code
responseHeaders.append(XMyAppLatency, start.durationToNow().toString())
` I will put the code in Github in a while.
4 Views