AutoHeadResponse
필수 의존성: io.ktor:ktor-server-auto-head-response
코드 예제: autohead
AutoHeadResponse 플러그인은 GET이 정의된 모든 라우트에 대해 HEAD 요청에 자동으로 응답하는 기능을 제공합니다. 실제 콘텐츠를 가져오기 전에 클라이언트에서 응답을 처리해야 하는 경우, AutoHeadResponse를 사용하여 별도의 head 핸들러를 생성하지 않아도 됩니다. 예를 들어, respondFile 함수를 호출하면 응답에 Content-Length와 Content-Type 헤더가 자동으로 추가되며, 클라이언트는 파일을 다운로드하기 전에 이 정보를 얻을 수 있습니다.
의존성 추가
AutoHeadResponse를 사용하려면 빌드 스크립트에 ktor-server-auto-head-response 아티팩트를 포함해야 합니다:
사용법
이 기능을 활용하려면 애플리케이션에 AutoHeadResponse 플러그인을 설치해야 합니다.
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 라우트는 해당 동사(verb)에 대한 명시적인 정의가 없더라도 이제 HEAD 요청에 응답합니다.
이 플러그인을 사용하는 경우, 동일한 GET 라우트에 대한 커스텀 HEAD 정의는 무시된다는 점에 유의해야 합니다.
옵션
AutoHeadResponse는 추가적인 구성 옵션을 제공하지 않습니다.
