kotlinhttprequesthttp-headersktor

How to get HTTP request headers in Ktor


I am using HttpClient from Ktor in my Kotlin project. How can I access the HTTP request headers? I know I can set them in the trailing HttpRequestBuilder lambda, but how do I see the headers that are currently set for the request (ideally before sending it)?


Solution

  • You can a phase to client's HttpRequestPipeline:

    val phase = PipelinePhase("Headers check")
    client.requestPipeline.addPhase(phase)
    client.requestPipeline.intercept(phase) {
        println("Headers: ${context.headers.entries()}")
    }