Facing issue with streamed response in kong

I am using Kong gateway to get streamed response, I have disabled response buffering at route level and I am getting response in streamed manner,But i have requirement to get the request body and response details for that I use Kong Functions (Pre-Plugins) with setting as below
kong.log.set_serialize_value(“request.body”, kong.request.get_raw_body())
kong.log.set_serialize_value(“response.body”, kong.response.get_raw_body())
when the plugin is used i am not getting streamed response, how to resolve the issue

Hey @Albin_Augustine

What you’re doing on the request is okay, but on the streamed response there is a catch.

When you call:

kong.response.get_raw_body()

then Kong will continue to read until the end of the stream, into RAM, and then return all-at-once to the client. You don’t want this, it seems like.

So what you do instead

In the header_filter and body_filter phases, you have two variables set automatically:

ngx.arg[1]  -- this is the current CHUNK
ngx.arg[2]  -- this is set to TRUE once this stream is completely finished

So in order to continue streaming back to the client, but also capture the full body, you should use e.g. this body_filter (annotated with comments - you can remove):

-- first make sure our storage var is initialised
kong.ctx.plugin.full_response = kong.ctx.plugin.full_response or ""

-- now capture the CURRENT chunk, tail it to the end of the last one we read
local current_chunk = ngx.arg[1] or ""
kong.ctx.plugin.full_response = kong.ctx.plugin.full_response .. current_chunk

-- are we FINISHED?
if ngx.arg[2] then
  -- now you have the FULL body in "kong.ctx.plugin.full_response" variable
  kong.log.debug("Response body: ", kong.ctx.plugin.full_response)
  kong.log.set_serialize_value("response.body", kong.ctx.plugin.full_response)
end

-- else, do nothing, the stream just continues back to client!

Remember that once you have started to read the first chunk, you have already responded to the client with 200 status (or whichever you set) and all headers, and it can’t be changed now.

2 Likes

Hey @JackGPT

Thank you for your response I tried the above and is working as expected without any issue.