# Reading request body in custom plugin

**URL:** <https://discuss.konghq.com/t/reading-request-body-in-custom-plugin/3018>\
**Category:** Questions\
**Created:** [March 14, 2019, 4:31pm UTC](https://discuss.konghq.com/t/reading-request-body-in-custom-plugin/3018 "2019-03-14T16:31:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![navin](https://avatars.discourse-cdn.com/v4/letter/n/e5b9ba/32.png) [@navin](https://discuss.konghq.com/u/navin)\
**Post date:** [March 14, 2019, 4:31pm UTC](https://discuss.konghq.com/t/reading-request-body-in-custom-plugin/3018/1 "2019-03-14T16:31:43Z")

</div>

**I am trying to read request body in a custom plugin**

```
  local data = kong.request.get_body()
  if data then
      kong.log(data)
  end

```

**I am getting the following error**

```
2019/03/14 21:57:55 [error] 14039#0: *45 lua entry thread aborted: runtime error: /usr/local/share/lua/5.1/kong/pdk/private/phases.lua:66: no phase in kong.ctx.core.phase
stack traceback:
coroutine 0:
        [C]: in function 'error'
        /usr/local/share/lua/5.1/kong/pdk/private/phases.lua:66: in function 'check_phase'
        /usr/local/share/lua/5.1/kong/pdk/request.lua:594: in function 'get_body'
        .../Apps/troop/kong/plugins/customlog/handler.lua:72: in function <.../Apps/troop/kong/plugins/customlog/handler.lua:62>, context: ngx.timer, client: 127.0.0.1, server: 0.0.0.0:8000

```

**Can anyone help me understand the problem here? I need to log the request body in my plugin.**

---

<div class="post-metadata">

**Author:** ![onematchfox](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/onematchfox/32/801_2.png) [@onematchfox](https://discuss.konghq.com/u/onematchfox)\
**Post date:** [March 15, 2019, 9:05am UTC](https://discuss.konghq.com/t/reading-request-body-in-custom-plugin/3018/2 "2019-03-15T09:05:45Z")

</div>

Looking at that error I’m going to guess that your code currently sits within the `new()` method in your plugin code. This is executed before the request context is available. Try moving your code to the appropriate phase handler (e.g. access) and it should work. E.g.

```auto
function PluginHandler:access(conf)
  PluginHandler.super.access(self)

  local data = kong.request.get_body()
  if data then
      kong.log(data)
  end

  return kong.response.exit(200)
end

```

See [https://docs.konghq.com/1.0.x/plugin-development/custom-logic/#available-request-contexts](https://docs.konghq.com/1.0.x/plugin-development/custom-logic/#available-request-contexts) for more information on the execution life-cycle (phases).

---

<div class="post-metadata">

**Author:** ![navin](https://avatars.discourse-cdn.com/v4/letter/n/e5b9ba/32.png) [@navin](https://discuss.konghq.com/u/navin)\
**Post date:** [March 15, 2019, 9:33am UTC](https://discuss.konghq.com/t/reading-request-body-in-custom-plugin/3018/3 "2019-03-15T09:33:49Z")

</div>

Thanks @onematchfox

I was calling **kong.request.get\_body()** in the **:log** handler
