is there any way to just write plain text into the...
# ktor
d
is there any way to just write plain text into the body of a post request with the ktor client? I have the following code
Copy code
val content = """
            {"identifier":"cdp_9055274"}
            {"identifier":"cdp_9111757"}
            {"identifier":"cdp_9135021"}
        """.trimIndent()

        val response = httpClient.patch<String> {
            url("/api/rest/v1/products")
            header("Authorization", "Bearer ${token.accessToken}")
            contentType(ContentType("application", "vnd.akeneo.collection+json"))
            body = content
        }
and it seems like regardless of what I'm writing into the body (the string, TextContent / ByteArrayContent), the body is always serialized into json again.
m
How is
httpClient
defined? Maybe you can try with a client where the JSON-feature isn't installed.
d
of course that would work.. it just feels pretty bad to have two clients injected because one of the requests requires a non-json body
Copy code
fun httpClient(objectMapper: ObjectMapper, settings: AkeneoClientSettings): HttpClient {
        return HttpClient(CIO) {
            defaultRequest {
                url {
                    host = settings.host
                    protocol = URLProtocol.HTTPS
                }
            }
            install(JsonFeature) {
                serializer = JacksonSerializer(objectMapper) {
                    propertyNamingStrategy = PropertyNamingStrategy.SNAKE_CASE
                }
            }
            install(Logging) {
                logger = Logger.SIMPLE
                level = LogLevel.ALL
            }
            expectSuccess = false
        }
    }
that's the client definition
and tbh i don't really understand why it's serializing the body again. The json feature should only kick-in for application/json content types
okay the last issue is explained by
value.startsWith("application/") && value.endsWith("+json")
m
Right, maybe you could set the Content-Type header to
text/plain
?
d
unfortunately the server rejects requests if the wrong content-type header is sent
m
Hmm, how is that wrong if what you want is to send plain text content?
d
Content-type • Equal to 'application/vnd.akeneo.collection+json', no other value allowed
because that's how it's documented.
m
Ok
d
In their definition it seems to be "some kind of json" if you have one json object serialized per line
instead of having a proper json array
m
What exactly happens to the body when you send the string with that content-type?
d
Copy code
HttpClient: CONTENT HEADERS
HttpClient: BODY Content-Type: application/vnd.akeneo.collection+json
HttpClient: BODY START
HttpClient: "{\"identifier\":\"cdp_9055274\"}\n{\"identifier\":\"cdp_9111757\"}\n{\"identifier\":\"cdp_9135021\"}"
okay what seems to work (for now) is the following
Copy code
install(JsonFeature) {
                serializer = JacksonSerializer(objectMapper) {
                    propertyNamingStrategy = PropertyNamingStrategy.SNAKE_CASE
                    receiveContentTypeMatchers = listOf(
                        object : ContentTypeMatcher {
                            override fun contains(contentType: ContentType): Boolean {
                                return ContentType.Application.Json.match(contentType)
                            }
                        }
                    )
                }
            }
i still think the design could be improved. I don't see a reason why we can't have a OutgoingContent implementation that is not touched by any serializer.. sth. like RawContent(body: ByteArray)
r
did you try something like this?
Copy code
val response = httpClient.patch<String> {
            url("/api/rest/v1/products")
            header("Authorization", "Bearer ${token.accessToken}")
            body = TextContent(content, ContentType("application", "vnd.akeneo.collection+json"))
        }
d
I did but it still applies the json serialization to the body
r
d
Of course. Do you just want me to describe my use-case? I'm not sure if one of the things I tried should have worked and is a bug or if everything works as intended and my use-case is simply not covered.
r
Example that I showed should not be intercepted by the serializer. But please make sure that you don’t set
ContentType
in header, only in
TextContent
. But I think that maybe it makes sense to skip serialization for all children of
OutgoingContent
. Ticket will be helpful ti think about usecases and agree on the solution