createClientPlugin

Creates a ClientPlugin that can be installed into an HttpClient.

The example below creates a plugin that adds a custom header to every request:

class CustomHeaderPluginConfig {
var headerName: String = "X-Custom-Header"
var headerValue: String = "Custom-Header-Value"
}
val CustomHeaderPlugin = createClientPlugin("CustomHeaderPlugin", ::CustomHeaderPluginConfig) {
val headerName = pluginConfig.headerName
val headerValue = pluginConfig.headerValue

onRequest { request, _ ->
request.headers.append(headerName, headerValue)
}
}

client.install(CustomHeaderPlugin) {
headerName = "X-Another-Header"
headerValue = "Another-Header-Value"
}

Parameters

name

A name of a plugin that is used to get its instance.

createConfiguration

Defines how the initial PluginConfigT of your new plugin can be created. Note that it may be modified later when a user of your plugin calls HttpClientConfig.install.

body

Allows you to define handlers (onRequest, onResponse, and so on) that can modify the behaviour of an HttpClient where your plugin is installed.


Creates a ClientPlugin with empty config that can be installed into an HttpClient.

The example below creates a plugin that adds a custom header to every request:

val CustomHeaderPlugin = createClientPlugin("CustomHeaderPlugin") {
onRequest { request, _ ->
request.headers.append("X-Custom-Header", "Custom-Header-Value")
}
}

client.install(CustomHeaderPlugin)

Parameters

name

A name of a plugin that is used to get its instance.

body

Allows you to define handlers (onRequest, onResponse, and so on) that can modify the behaviour of an HttpClient where your plugin is installed.