Why does the server do not have a body that can be...
# ktor
h
Why does the server do not have a body that can be set like the client (pipeline/message pattern) but directly send it out to the response via
pipeline.execute
?
a
Can you please share some code to illustrate your issue?
h
Well, the client api allows you to call
setBody = "foo"
and later
setBody = "bar"
multiple times, and
HttpStatement.execute
uses the latest body to start the pipeline. On the server, you do have
ApplicationCall
, but you cannot set the body multiple times, because the server does not execute the pipeline automatically (if you forgot calling
respond
, you get 404) and
respond
executes the server pipeline immediately, so you cannot set the body twice (and only sending the latest body). Or more general: the client api follows the message pattern that allows you to fully transform the message (HttpRequestBuilder) multiple times before sending, while the server sends the body directly.
a
This is likely both historical and a performance choice. The client was built around "transform the message, then send," while the server treats
respond()
as the final step. And since a server handles many connections at once, with bodies often streamed rather than buffered, letting you replace a body afterward would mean holding the whole thing in memory, so it sends the body directly instead.
🙏 1