Hey! Regarding client `ContentNegotiation` plugin:...
# ktor
j
Hey! Regarding client
ContentNegotiation
plugin: I need access to the serialised body (as
TextContent
) in order to calculate a header value: The api I’m using expects a signature header which needs the body content for calculating the signature value 🙃. Snipped of what I’m trying to do in
HttpRequestPipeline.Send
phase:
private val HttpRequestBuilder.bodyAsText get() = (body as? TextContent)?.text
(Prior to send the body is not available as text) Problem: I cannot set headers anymore after the body had been serialised, is that correct? Do you know a workaround?
✔️ 1
a
I suppose the issue there is partly that Ktor will try to stream the response body after it wrote the headers, so at no point is the entire body available in-memory for signature calculations. So maybe you need to do content negotiation by hand and manually set content-type and the response body? 🤔
👍 1
for some reason, the functions to parse the request headers and return the sorted list of accepted content types, is internal to the plugin. If it wasn’t, you could have used that to do the negotiation manually https://github.com/ktorio/ktor/blob/d300441cb70b6a590ebe263febc36f3fe825b4f6/ktor-[…]or/server/plugins/contentnegotiation/ContentNegotiationUtils.kt
👀 1
j
Yep, that’s a good solution. I don’t use the content negotiation plugin, but do the body serialisation , bode deserialisation and content type header setup manually now (that isn’t too complicated anyway). With that I can calculate the signature, as I can define when the body type gets serialised. Thanks for your hint 🙏
🎉 1