https://kotlinlang.org logo
#ktor
Title
# ktor
g

Gyeongmin Go

03/22/2019, 8:54 AM
Hi everyone, while I develop something with ktor-client and JsonFeature enabled, I wonder is there any way to allow not only "application/json" but for more content types, like "application/x-json". For now, I forked JsonFeature to my project code and added newly defined content-type, and modified code a bit(attach the code fragments below).
Copy code
// previous code, io.ktor.client.features.json.JsonFeature
// JsonFeature.Feature object, and install function
            scope.requestPipeline.intercept(HttpRequestPipeline.Transform) { payload ->

                context.accept(ContentType.Application.Json)
                if (context.contentType()?.match(ContentType.Application.Json) != true) {
                    return@intercept
                }

                context.headers.remove(HttpHeaders.ContentType)
                proceedWith(feature.serializer.write(payload))
            }
Copy code
// forked code
// defined possible content types inside Feature object,
            private val allowedContentTypes = listOf(
            ContentType.Application.Json,
            ContentType.parse("application/x-json")
        )
            scope.requestPipeline.intercept(HttpRequestPipeline.Transform) { payload ->
                allowedContentTypes.forEach { context.accept(it) }

                if (allowedContentTypes.none { context.contentType()?.match(it) == true })
                    return@intercept

                context.headers.remove(HttpHeaders.ContentType)
                proceedWith(feature.serializer.write(payload))
            }
e

e5l

03/22/2019, 8:56 AM
Hi @Gyeongmin Go, there are no such option for now. Probably we should implement it like server
Content-Negotiation
feature.
btw PRs are always welcome 🙂
👍 1
g

Gyeongmin Go

03/22/2019, 8:59 AM
Thanks for your fast reply. I'll try PR... if I can...:)