Ktor native request inteceptor

Viewed 951

I need to add a header to requests based on the body. How can I add interceptor for client requests? I have tried

 httpClient.requestPipeline.intercept(HttpRequestPipeline.Send) { context: PipelineContext<Any, HttpRequestBuilder> ->
            context.proceed()
}

But it wasn't called

1 Answers

You can use the State phase of the HttpSendPipeline to modify request:

val client = HttpClient(Apache)

client.sendPipeline.intercept(HttpSendPipeline.State) {
    context.headers.append("myHeader", context.body.toString())
}

val r = client.get<String>("http://httpbin.org/get")
println(r)
Related