Ktor 3.0.0-beta-1 Help

AutoHeadResponse

The AutoHeadResponse plugin provides us with the ability to automatically respond to a HEAD request for every route that has a GET defined. You can use AutoHeadResponse to avoid creating a separate head handler if you need to somehow process a response on the client before getting the actual content. For example, calling the respondFile function adds the Content-Length and Content-Type headers to a response automatically, and you can get this information on the client before downloading the file.

Add dependencies

To use AutoHeadResponse, you need to include the ktor-server-auto-head-response artifact in the build script:

implementation("io.ktor:ktor-server-auto-head-response:$ktor_version")
implementation "io.ktor:ktor-server-auto-head-response:$ktor_version"
<dependency> <groupId>io.ktor</groupId> <artifactId>ktor-server-auto-head-response-jvm</artifactId> <version>${ktor_version}</version> </dependency>

Usage

In order to take advantage of this functionality, we need to install the AutoHeadResponse plugin in our application.

import io.ktor.server.application.* import io.ktor.server.plugins.autohead.* import io.ktor.server.response.* import io.ktor.server.routing.* fun Application.main() { install(AutoHeadResponse) routing { get("/home") { call.respondText("This is a response to a GET, but HEAD also works") } } }

In our case the /home route will now respond to HEAD request even though there is no explicit definition for this verb.

It's important to note that if we're using this plugin, custom HEAD definitions for the same GET route will be ignored.

Options

AutoHeadResponse does not provide any additional configuration options.

Last modified: 02 April 2024