Skip to content
Server Plugin

AutoHeadResponse

필수 의존성: io.ktor:ktor-server-auto-head-response

코드 예제: autohead

네이티브 서버Ktor는 Kotlin/Native를 지원하므로 추가 런타임이나 가상 머신 없이도 서버를 실행할 수 있습니다. 지원: ✅

AutoHeadResponse 플러그인은 GET이 정의된 모든 라우트에 대해 HEAD 요청에 자동으로 응답하는 기능을 제공합니다. 실제 콘텐츠를 가져오기 전에 클라이언트에서 응답을 미리 처리해야 하는 경우, AutoHeadResponse을 사용하여 별도의 head 핸들러를 만들지 않고 처리할 수 있습니다. 예를 들어, respondFile 함수를 호출하면 응답에 Content-Length 및 Content-Type 헤더가 자동으로 추가되므로, 클라이언트는 파일을 다운로드하기 전에 이 정보를 미리 확인할 수 있습니다.

의존성 추가 ​

AutoHeadResponse을 사용하려면 빌드 스크립트에 ktor-server-auto-head-response 아티팩트를 추가하세요:

kotlin
implementation(ktorLibs.server.autoHeadResponse)
위 코드 스니펫은 Ktor 의존성을 지정하기 위해 Ktor 버전 카탈로그를 사용합니다.
groovy
implementation ktorLibs.server.autoHeadResponse
위 코드 스니펫은 Ktor 의존성을 지정하기 위해 Ktor 버전 카탈로그를 사용합니다.
xml
<dependency>
    <groupId>io.ktor</groupId>
    <artifactId>ktor-server-auto-head-response-jvm</artifactId>
    <version>${ktor_version}</version>
</dependency>

사용법 ​

이러한 기능을 활용하려면 애플리케이션에 AutoHeadResponse 플러그인을 설치해야 합니다.

kotlin
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")
        }
    }
}

이 경우 /home 라우트는 해당 HTTP 메서드(verb)에 대한 명시적인 정의가 없더라도 HEAD 요청에 응답합니다.

이 플러그인을 사용할 때 동일한 GET 라우트에 대해 별도로 정의한 커스텀 HEAD 정의는 무시된다는 점에 유의해야 합니다.

옵션 ​

AutoHeadResponse는 추가적인 설정 옵션을 제공하지 않습니다.