Plugin Response phase - how to set response body?

Kong 2.2.0 introduces a new response phase that according to the changelog allows to manipulate both the response headers and the response body in the same phase.

However, while kong.service.response.get_raw_body() and kong.service.response.get_body() are supported in this phase, there is no kong.response.set_raw_body() nor kong.response.set_body().

How can the response body be manipulated in the response phase?


If you look at Kong’s source for response.lua, you’ll see that they’ve, well, considered the PDK function.

For body_filter plugins historically, you would set the response body by setting ngx.arg[1] to the body bytes you wanted to send, typically when ngx.arg[2] was not nil (body_filter plugin phases being called potentially multiple times). Have you tried doing the same for a response phase plugin?

Yes, I tried. But ngx.arg is only available in the set_by_lua and body_filter_by_lua phases.
The error I received is API disabled in the context of access_by_lua*

Worth a shot. My company’s custom plugins haven’t explored the response phase yet, so someone from Kong will have to respond here.

1 Like

How about this?
return kong.response.exit(kong.response.get_status(), ‘new body’, kong.response. get_headers())

kong.response.exit will prevent form other plugins to run in the current phase. It is not suitable with our needs.

© 2019 Kong Inc.    Terms  •  Privacy  •  FAQ