Hi, I have a silly question about intercepting HTT...
# ktor
r
Hi, I have a silly question about intercepting HTTP calls with Ktor client. I see the documentation (https://ktor.io/docs/client-http-send.html) suggest to jump onto HttpSend plugin after configuring the client. But, using a custom plugin (https://ktor.io/docs/client-custom-plugins.html#example-custom-header) seems to lead to the same result using
onRequest
. Maybe there is a semantic explanation for both usage, but at the end what do you suggest to use ?
a
client.plugin(HttpSend).intercept {}
is the same as
on(Send) {}
and happens during the actual sending of the request. The
onRequest
is executed during the request transformation. Can you describe the problem you're trying to solve?
🙏 1
r
I am looking at how to properly configure my client and just want to know which practice is better. I have a few use cases, like : 1. adding some headers for each request, depending on some HttpHeaderProvider class. 2. setting headers and cookies from a specific source (in addition to the HttpCookie plugin) 3. extracting cookies from any responses to save them (and re-inject them in 2.) So my first approach was using HttpSend for (1), and create my own plugin for (2) and (3). But, again I found both approach very similar, so I wanted to ask which one is preferable? (even if
HttpSend
act on during the send phase, it does allow us to transform the request? so I am still confused about which approach is better).
a
The main difference between
on(Send)
and
onRequest
is that
on(Send)
is executed on each actual sending of the request, but
onRequest
is only once per
client.request(...)
call. For example, if the client follows redirects and the server responds with 301 or 302, the
on(Send)
will be executed for the initial request and for each redirection request made internally by the
HttpRedirect
plugin. On the other hand,
onRequest
will be executed only once on the initial request.
r
Oh! Ok, that makes perfect sense! So, I need to ask myself when do I need my configuration to happened! TY 🙏