Hi,
From above what is the way to set a transformed body to the kong.response ? Say if I have LUA json table that I owuld like to set on the response using a custom plugin.
Thanks.
Hi,
From above what is the way to set a transformed body to the kong.response ? Say if I have LUA json table that I owuld like to set on the response using a custom plugin.
Thanks.
kong.response.exit will allow you to return any kind of response back to the client. (e.g. kong.response.exit(200, {"foo" : "bar"}))
For many use cases, the response-transformer at https://docs.konghq.com/hub/kong-inc/response-transformer/ is enough. Just making sure you know about of it.
Hi @Raimon_Grau thanks for that.
My use case is to transform a complex Json from the backend service response into a slightly different format before being sent downstream.
The Json has nested structures so the basic response-transformer wasn’t enough for me or I didnt know how to use it for this.
I have scenarios like to drop a top level json name but keep contents inside also.
Thanks.
@saravana Please have a look at the code of the response-transformer plugin:
This should you give an idea of how to use the cjson module and Lua to drop the JSON attribute.
I added this in my post-function in body_filter to set result key as nil, as a test:
local cjson = require(“cjson”) local body = cjson.decode(kong.response.get_raw_body()) body.result = nil return cjson.encode(body)
But kong is throwing error
require ‘cjson’ not allowed within sandbox
So how can we write a manual body_filter lua code to remove keys from a complex json?