# Plugin Response phase - how to set response body?

**URL:** <https://discuss.konghq.com/t/plugin-response-phase-how-to-set-response-body/7937>\
**Category:** General\
**Created:** [February 22, 2021, 7:49am UTC](https://discuss.konghq.com/t/plugin-response-phase-how-to-set-response-body/7937 "2021-02-22T07:49:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ayala](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/ayala/32/1601_2.png) [@ayala](https://discuss.konghq.com/u/ayala)\
**Post date:** [February 22, 2021, 7:49am UTC](https://discuss.konghq.com/t/plugin-response-phase-how-to-set-response-body/7937/1 "2021-02-22T07:49:26Z")

</div>

Kong 2.2.0 introduces [a new response phase](https://github.com/Kong/kong/blob/2.3.2/CHANGELOG.md#plugins-3) that according to the changelog allows to **manipulate both the response headers and the response body** in the same phase.

However, while `kong.service.response.get_raw_body()` and `kong.service.response.get_body()` are supported in this phase, there is no `kong.response.set_raw_body()` nor `kong.response.set_body()`.

How can the response body be manipulated in the response phase?

Thanks

---

<div class="post-metadata">

**Author:** ![tyree731](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/tyree731/32/1374_2.png) [@tyree731](https://discuss.konghq.com/u/tyree731)\
**Post date:** [February 23, 2021, 3:01pm UTC](https://discuss.konghq.com/t/plugin-response-phase-how-to-set-response-body/7937/2 "2021-02-23T15:01:31Z")

</div>

If you look at Kong’s source for [response.lua](https://github.com/Kong/kong/blob/master/kong/pdk/response.lua#L521), you’ll see that they’ve, well, considered the PDK function.

For `body_filter` plugins historically, you would set the response body by setting `ngx.arg[1]` to the body bytes you wanted to send, typically when `ngx.arg[2]` was not `nil` (`body_filter` plugin phases being called potentially multiple times). Have you tried doing the same for a response phase plugin?

---

<div class="post-metadata">

**Author:** ![ayala](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/ayala/32/1601_2.png) [@ayala](https://discuss.konghq.com/u/ayala)\
**Post date:** [February 24, 2021, 9:09am UTC](https://discuss.konghq.com/t/plugin-response-phase-how-to-set-response-body/7937/3 "2021-02-24T09:09:38Z")

</div>

Yes, I tried. But `ngx.arg` is only available in the `set_by_lua` and `body_filter_by_lua` phases.  
The error I received is `API disabled in the context of access_by_lua*`

---

<div class="post-metadata">

**Author:** ![tyree731](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/tyree731/32/1374_2.png) [@tyree731](https://discuss.konghq.com/u/tyree731)\
**Post date:** [February 24, 2021, 8:00pm UTC](https://discuss.konghq.com/t/plugin-response-phase-how-to-set-response-body/7937/4 "2021-02-24T20:00:52Z")

</div>

Worth a shot. My company’s custom plugins haven’t explored the response phase yet, so someone from Kong will have to respond here.

---

<div class="post-metadata">

**Author:** ![Mason](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/mason/32/2696_2.png) [@Mason](https://discuss.konghq.com/u/Mason)\
**Post date:** [May 12, 2021, 8:07am UTC](https://discuss.konghq.com/t/plugin-response-phase-how-to-set-response-body/7937/5 "2021-05-12T08:07:15Z")

</div>

How about this?  
return kong.response.exit(kong.response.get\_status(), ‘new body’, kong.response. get\_headers())

---

<div class="post-metadata">

**Author:** ![ayala](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/ayala/32/1601_2.png) [@ayala](https://discuss.konghq.com/u/ayala)\
**Post date:** [May 19, 2021, 12:35pm UTC](https://discuss.konghq.com/t/plugin-response-phase-how-to-set-response-body/7937/6 "2021-05-19T12:35:16Z")

</div>

`kong.response.exit` will prevent form other plugins to run in the current phase. It is not suitable with our needs.

---

<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 8, 2022, 1:25pm UTC](https://discuss.konghq.com/t/plugin-response-phase-how-to-set-response-body/7937/7 "2022-10-08T13:25:43Z")

</div>

Is it resolved please?

---

<div class="post-metadata">

**Author:** ![Fuxin\_Hao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/fuxin_hao/32/4009_2.png) [@Fuxin\_Hao](https://discuss.konghq.com/u/Fuxin_Hao)\
**Post date:** [September 8, 2023, 8:34am UTC](https://discuss.konghq.com/t/plugin-response-phase-how-to-set-response-body/7937/8 "2023-09-08T08:34:51Z")

</div>

any updates for this?
