Client plugins
Many applications require common functionality that is not part of the core application logic, such as logging, serialization, or authorization. In Ktor, this functionality is provided by client plugins.
Add plugin dependencies
Some plugins require an additional dependency. For example, to use the Logging plugin, you need to add the ktor-client-logging artifact in your build script:
Each plugin’s documentation specifies any required dependencies.
Install a plugin
To install a plugin, pass it to the install() function inside a client configuration block.
For example, installing the Logging plugin looks as follows:
Install or replace a plugin
In some cases, a plugin may already be installed — for example, by shared client configuration code. In such cases, you can replace its configuration using the installOrReplace() function:
This function installs the plugin if it is not present or replaces its existing configuration if it has already been installed.
Configure a plugin
Most plugins expose configuration options that can be set inside the install block.
For example, the Logging plugin allows you to specify the logger, logging level, and condition for filtering log messages:
Create a custom plugin
If the existing plugins do not meet your needs, you can create your own custom client plugins. Custom plugins allow you to intercept requests and responses and implement reusable behavior.
To learn more, see Custom client plugins.