Not able to modify response in Kong open source

Hi There,

I have a usecase where I need to modify the upstream service response before sending it back to the client application. I am trying to use Post Function to edit the response. But facing challenges to parse and build new JSON.

Can you please help me to achieve this in Kong open source docker version?

Thanks,
Nitiraj

@nitiraj_pachkale Does the Request Transformer plugin work for your case? If not, please provide more details in your question so we can try and assist with the issue. Include any example configuration and steps to reproduce. Thanks

Hi @rick,

Thanks for your reply.

Yes, request trasformer is working fine with both “Request Transformer” plugin and “Pre Function” plugin with Lua Script. I am able to send modified request to upsteam service. But facing problem while I am trying to modify response returned by upstream service.

I am trying to use Lua Script in response flow. Please find below the same -

local cjson = require “cjson”
local body = kong.response.get_raw_body()
local json_body = cjson.decode(body)
local json_obj = json_body.json
local new_json_body = cjson.encode(json_obj)
kong.response.set_raw_body(new_json_body)

Basically there is a key with name “json” in response body. I want to extract that and set it as new response body.

I am using Post Function Plugin with Lua script. Phase - body_filter

Please help me to undestand how we can achieve this using Lua Scritp.

Thanks,
Nitiraj

@nitiraj_pachkale Can you please provide the current behavior or error you observe with your existing code?

This issue is resolved. I built custom plugin using lua to modify response.

Thanks,
Nitiraj