Hello everyone. I'm implementing a debug view in m...
# ktor
l
Hello everyone. I'm implementing a debug view in my app and i need to intercept all the Http responses to list them in this view. The information i want to save are: status code, headers, timestamp and the response body (as plain text), how could i do this? [SOLVED ✅ ]
I tried using this
responseValidator
inside my HttpResponseValidator but whenever i try to use
response.body
a json serialization error is thrown
Update: I moved this listener implementation to the
ResponseObserver
plugin but the problem remains
image.png
Nevermind,
bodyAsText
is working as expected when used inside ResponseObserver
👍 1
s
Hello, the problem with ‘ResponseObserver’ is that it only treats response. I’m trying to install a plug-in that reads both request and response for logging purpose, such as Zalando’logbook-ktor-client plugin. However it seems really tricky to read request/rĂ©ponse inside custom plugin and the only way I found was to use the ‘split’ method from the unmodified ‘content’ in the ‘HttpResponse’ object. The problem with that too is that ‘content’ is an internal API.. So what is the official way to read bodies inside custom plugin? The official documentation only mention headers, etc.. but not body.
l
Good to know
But my use case only requires response level logs
s
Yep I will probably open a thread about this on Monday
l
Hey, I'm pretty sure it's possible to read the request using the Response Observer
The
OnResponse
callback provides an instance of
HttpResponse
, and this class has a member named
request
Isn't this what you looking for?
s
Hmm interesting. But is the observer called if you don’t receive the response ? (When it times out for example)
Also an inconvenient I can see is that the log timestamps will be the same for both request and response unless there’s a way to retrieve the request’s call timestamp
l
I believe time outs are handled by ktor with internal exceptions so it shouldn't be a problema