# How to modify response Body

**URL:** <https://discuss.konghq.com/t/how-to-modify-response-body/4360>\
**Category:** Questions\
**Created:** [September 4, 2019, 12:29pm UTC](https://discuss.konghq.com/t/how-to-modify-response-body/4360 "2019-09-04T12:29:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![prkshdalmia](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@prkshdalmia](https://discuss.konghq.com/u/prkshdalmia)\
**Post date:** [September 4, 2019, 12:29pm UTC](https://discuss.konghq.com/t/how-to-modify-response-body/4360/1 "2019-09-04T12:29:40Z")

</div>

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”.

---

<div class="post-metadata">

**Author:** ![jeremyjpj0916](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/jeremyjpj0916/32/1388_2.png) [@jeremyjpj0916](https://discuss.konghq.com/u/jeremyjpj0916)\
**Post date:** [September 4, 2019, 2:59pm UTC](https://discuss.konghq.com/t/how-to-modify-response-body/4360/2 "2019-09-04T14:59:13Z")

</div>

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:

> **[Open-Source API Management and Microservice Management](https://docs.konghq.com/hub/kong-inc/response-transformer/)**
>
> Secure, Manage & Extend your APIs or Microservices with plugins for authentication, logging, rate-limiting, transformations and more.

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.

Cheers,  
Jeremy

---

<div class="post-metadata">

**Author:** ![prkshdalmia](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@prkshdalmia](https://discuss.konghq.com/u/prkshdalmia)\
**Post date:** [September 5, 2019, 7:01am UTC](https://discuss.konghq.com/t/how-to-modify-response-body/4360/3 "2019-09-05T07:01:11Z")

</div>

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`

 ![Screenshot%20(5)](https://canada1.discourse-cdn.com/flex036/uploads/konghq/original/2X/0/0cb55d57666d4d89c89fdf94ffa2211a09d1306e.png)

---

<div class="post-metadata">

**Author:** ![jeremyjpj0916](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/jeremyjpj0916/32/1388_2.png) [@jeremyjpj0916](https://discuss.konghq.com/u/jeremyjpj0916)\
**Post date:** [September 5, 2019, 8:19pm UTC](https://discuss.konghq.com/t/how-to-modify-response-body/4360/4 "2019-09-05T20:19:46Z")

</div>

Yep, that logic starts here in the existing plugin(body\_filter phase):

> <https://github.com/Kong/kong/blob/master/kong/plugins/response-transformer/handler.lua#L20>

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

---

<div class="post-metadata">

**Author:** ![prkshdalmia](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@prkshdalmia](https://discuss.konghq.com/u/prkshdalmia)\
**Post date:** [September 6, 2019, 5:59am UTC](https://discuss.konghq.com/t/how-to-modify-response-body/4360/5 "2019-09-06T05:59:11Z")

</div>

Thankx Jeremy. I think this will do the job.

---

<div class="post-metadata">

**Author:** ![sivasubramani30](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@sivasubramani30](https://discuss.konghq.com/u/sivasubramani30)\
**Post date:** [October 7, 2022, 11:30am UTC](https://discuss.konghq.com/t/how-to-modify-response-body/4360/6 "2022-10-07T11:30:05Z")

</div>

Hi,  
Is it worked for you?
