ktor
ktor copied to clipboard
How to intercept and edit in a http request the body using ktor-client (KMM)
My task is to add to every http post request, which is being send via the ktor client, specific parameters to the body. When I was using retrofit, I did this easily by using Interceptors. Due to switch to KMM, we need to convert those interceptors to kTor.
I tried it but it failed until now and currently I'm a little bit helpless how to fix this issue.
Hey, I think you have 3 ways to handle that:
- Create a class serializable (or the content type plugin used) for each use case (recommended)
- Use a custom plugin for transformation body before send request. Tip: Use an attribute key if the condition is more manual and can't catch with the before custom plugin config.
- Use resource plugin to more type safe
My advice is use inheritance or interfaces to implement different classes with each use case, more semantic because each class means X use case, inheritance to reutilize fields and/or funcitons, interfaces if have more than one parent class.