Kong / kong-python-pdk

Write Kong plugins in Python (Experimental)
Apache License 2.0
40 stars 14 forks source link

Support for manipulating response body in Python PDK #128

Open Hicham-Chahboune opened 11 months ago

Hicham-Chahboune commented 11 months ago

Hello,

I'm developing a Kong plugin using the Python PDK. One of the requirements of my plugin is to manipulate the response body, similar to how it's done in the Lua PDK with ngx.arg[1] during the body_filter phase. (or response.set_raw_body)

However, I've noticed that the Python PDK doesn't seem to support the body_filter phase or provide a direct way to interact with the response body in chunks

fffonion commented 11 months ago

Hi @Hicham-Chahboune , response.set_raw_body is not supported in external plugins, however you can consider using response.exit instead.

Hicham-Chahboune commented 11 months ago

Thank you for your response. The issue with "response.exit" is that it interrupts the execution process of plugins. When a plugin calls "response.exit," it prevents the process from passing to other plugins

fffonion commented 11 months ago

@Hicham-Chahboune That is correct. Unfortunately that's the limitation we have.

Hicham-Chahboune commented 11 months ago

And with Lua, is it possible to change the content of the body? I tried to assign a new value to ngx.arg[1], but it doesn't work. When I manipulate the content without changing it, it works (for example, using ngx.arg[1] = string.upper(body) works). However, when I try to assign a new variable like newbody, it does not work (for example, using ngx.arg[1] = newbody)

fffonion commented 11 months ago

yes in lua it's supported, the more detailed documentation is at openresty side (ref: https://github.com/openresty/lua-nginx-module#body_filter_by_lua_block)