ktor icon indicating copy to clipboard operation
ktor copied to clipboard

How to intercept and edit in a http request the body using ktor-client (KMM)

Open ArturoRen opened this issue 5 months ago • 1 comments

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.

ArturoRen avatar Aug 07 '25 02:08 ArturoRen

Hey, I think you have 3 ways to handle that:

  1. Create a class serializable (or the content type plugin used) for each use case (recommended)
  2. 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.
  3. 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.

kingg22 avatar Aug 11 '25 23:08 kingg22