https://kotlinlang.org logo
Title
c

christophsturm

07/22/2021, 12:59 PM
is there a way to send a http request without accept header with ktor client? I need it in my test suite to check that its handled correctly
a

Aleksei Tirman [JB]

07/22/2021, 1:17 PM
You can do it with a little bit of magic :magic_wand: by intercepting a request pipeline just after
Render
phase and removing
Accept
header:
val client = HttpClient(Apache)

val newPhase = PipelinePhase("phase")
client.requestPipeline.insertPhaseAfter(HttpRequestPipeline.Render, newPhase)
client.requestPipeline.intercept(newPhase) {
    context.headers.remove("Accept")
}

val r = client.get<String>("<https://httpbin.org/get>")
println(r)
👍 1
c

christophsturm

07/22/2021, 1:33 PM
thank you. it is a totally valid usecase imo, sending requests that are not 100% wellformed is necessary to for testing, so maybe this can be made easier
👍🏽 1
👍 1
h

Helio

07/19/2022, 2:02 AM
Does any of you know if this was improved or if this :magic_wand: is still the best option for Ktor v2? I thought an improvement was implemented at: https://github.com/ktorio/ktor/pull/1908/files but I can’t find any documentation about it in Ktor?