Kong / kong-python-pdk

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

Example of how to change the response body #109

Closed nagyrobir closed 1 year ago

nagyrobir commented 1 year ago

I am trying to familiarize myself with kong-python-pdk, but i need some help with understanding exactly how to use functions which are present in the KONG-PDK documentation and which are not available in the kong-python-pdk. Most of the tutorials out there focus on editing headers/access but not editing the body of the response Kong will send back to the requestor.

For example i want to find and replace any instances of a substring within a response, independently of the json key. For this, i am quite unsure, but i presume i could use the response.get_body() do the modifications with string matching and the use response.set_body() to set the new response. The issue is that i cannot find a set_body() function,

I would be very grateful if somebody could provide a simple implementation of the example that i mentioned above. Maybe others like I could benefit from this. If core developers don't mind, in order to familiarize others with this tool i could make a blogpost/youtube tutorial on how to use these tools.

thank you!

fffonion commented 1 year ago

Hi @nagyrobir , the kong.response.set_body() API currently doesn't work in external plugins, only Lua plugins have this API. You can see the full list of PDK API supported at here (https://kong.github.io/kong-python-pdk/), it's searchable and up-todate.

However, if you only want to set the response body once and exit, you can use kong.response.exit.

nagyrobir commented 1 year ago

Thank you a lot for your answer. Allthough i am not experienced in Lua, i managed to do it somehow. Cheers!