How to modify response Body

I wanted to modify/edit/replace the response body when there is an Error in the upstream response.

For Example:
When there is a 404 error; I want to edit the response body as “Hey Something is Fishy. Try checking the path”.

As far as open source plugins go I believe right now you would need to write a custom plugin checking the HTTP Status from your backend’s and then setting the response body to configured messages(that could be stored in the plugin schema as well) per Status.

I assume this because the response transformer:

Is all based on replacing X with Y, when you want if(Z) replace X with Y, they might accept a new config option like config.only_on_status maybe that if it was set to various HTTP Status numbers that would cause the response body to be replaced.


Thanks Jeremy,
Yes I constructing a custom Plugin only. But I wanted to know the kong command to modify the response body.
like for changing response header we use kong.response.set_header(). Do we have anything for set_body

Yep, that logic starts here in the existing plugin(body_filter phase):

Edit- Removed PDK link as I don’t think it works for that case.

Thankx Jeremy. I think this will do the job.