Read response content in Response.OnHeaders()


Is it possible to read/store the response content immediately before Sparkle sends it back to the client? I was trying to use the C.Response.OnHeaders() proc, but although the Resp.ContentLength is reported, the content stream appears to be empty. I looked at the logging middleware, and that doesn't appear to support logging the response content, so maybe it's not possible?


The content is sent "on-the-fly", when OnHeaders is called, the response is not necessarily set yet - it might still be written by the framework via streams. It is possible to get the response content, but it's not trivial, you will have to replace the response stream by your own and then capture the data as it's written.

The source code of compress middleware (unit Sparkle.Middleware.Compress) is an example of how to do that.