diff --git a/ktor-client/ktor-client-plugins/ktor-client-content-negotiation/common/src/io/ktor/client/plugins/contentnegotiation/ContentNegotiation.kt b/ktor-client/ktor-client-plugins/ktor-client-content-negotiation/common/src/io/ktor/client/plugins/contentnegotiation/ContentNegotiation.kt index c2f7efb5d76..ceb4a80d0dd 100644 --- a/ktor-client/ktor-client-plugins/ktor-client-content-negotiation/common/src/io/ktor/client/plugins/contentnegotiation/ContentNegotiation.kt +++ b/ktor-client/ktor-client-plugins/ktor-client-content-negotiation/common/src/io/ktor/client/plugins/contentnegotiation/ContentNegotiation.kt @@ -140,6 +140,8 @@ public class ContentNegotiation internal constructor( internal suspend fun convertRequest(request: HttpRequestBuilder, body: Any): Any? { registrations.forEach { LOGGER.trace("Adding Accept=${it.contentTypeToSend.contentType} header for ${request.url}") + + if (request.headers.contains(HttpHeaders.Accept, it.contentTypeToSend.toString())) return@forEach request.accept(it.contentTypeToSend) } diff --git a/ktor-client/ktor-client-plugins/ktor-client-content-negotiation/ktor-client-content-negotiation-tests/jvm/src/io/ktor/client/plugins/contentnegotiation/tests/JsonContentNegotiationTest.kt b/ktor-client/ktor-client-plugins/ktor-client-content-negotiation/ktor-client-content-negotiation-tests/jvm/src/io/ktor/client/plugins/contentnegotiation/tests/JsonContentNegotiationTest.kt index abf2dbf448c..91b0cacbdee 100644 --- a/ktor-client/ktor-client-plugins/ktor-client-content-negotiation/ktor-client-content-negotiation-tests/jvm/src/io/ktor/client/plugins/contentnegotiation/tests/JsonContentNegotiationTest.kt +++ b/ktor-client/ktor-client-plugins/ktor-client-content-negotiation/ktor-client-content-negotiation-tests/jvm/src/io/ktor/client/plugins/contentnegotiation/tests/JsonContentNegotiationTest.kt @@ -280,6 +280,25 @@ abstract class JsonContentNegotiationTest(val converter: ContentConverter) { } } + @Test + fun testNoDuplicatedHeaders() = testApplication { + install(ContentNegotiation) { + register(ContentType.Application.Json, converter) + } + + createClient { + install(io.ktor.client.plugins.contentnegotiation.ContentNegotiation) { + register(ContentType.Application.Json, converter) + } + }.get { + header(HttpHeaders.Accept, "application/json") + }.let { response -> + response.request.headers.forEach { _, values -> + assertEquals(1, values.size) + } + } + } + @Test open fun testRespondNestedSealedWithTypeInfoAny() = testApplication { install(ContentNegotiation) {