Skip to content

Latest commit

 

History

History
69 lines (49 loc) · 2.97 KB

double-receive.md

File metadata and controls

69 lines (49 loc) · 2.97 KB

Required dependencies: io.ktor:%artifact_name%

The %plugin_name% plugin provides the ability to receive a request body several times with no RequestAlreadyConsumedException exception. This might be useful if a plugin is already consumed a request body, so you cannot receive it inside a route handler. For example, you can use %plugin_name% to log a request body using the CallLogging plugin and then receive a body one more time inside the post route handler.

The %plugin_name% plugin uses an experimental API that is expected to evolve in the upcoming updates with potentially breaking changes.

{type="note"}

Add dependencies {id="add_dependencies"}

Install %plugin_name% {id="install_plugin"}

After installing %plugin_name%, you can receive a request body several times and every invocation returns the same instance. For example, you can enable logging of a request body using the CallLogging plugin...

{src="snippets/double-receive/src/main/kotlin/com/example/Application.kt" include-lines="16-23"}

... and then get a request body one more time inside a route handler.

{src="snippets/double-receive/src/main/kotlin/com/example/Application.kt" include-lines="25-28"}

You can find the full example here: double-receive.

Configure %plugin_name% {id="configure"}

With the default configuration, %plugin_name% provides the ability to receive a request body as the following types:

  • ByteArray
  • String
  • Parameters
  • data classes used by the ContentNegotiation plugin

By default, %plugin_name% doesn't support:

Set the cacheRawRequest property to false, if you do not need to receive different types from the same request or receive a stream or channel:

install(DoubleReceive) {
    cacheRawRequest = false
}