【问题标题】:Kong API Gateway - Reading request body in custom pluginKong API Gateway - 在自定义插件中读取请求正文
【发布时间】:2019-08-05 15:58:54
【问题描述】:

我正在尝试通过关注 this url

来读取自定义插件中的请求正文
  local data = kong.request.get_body()
  if data then
      kong.log(data)
  end

我收到以下错误

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.

谁能帮我理解这里的问题?我需要在我的插件中记录请求正文。

【问题讨论】:

    标签: nginx openresty kong lua-ngx-module kong-plugin


    【解决方案1】:

    该调用只能在“rewrite、access or admin_api”阶段调用。

    查看您的日志输出,您尝试调用它的上下文是 ngx.timer,它甚至没有被 Kong 包装为其上下文之一。 (更多关于here

    你可以做的是在一个可以调用它的阶段调用“kong.request.get_body()”,将它存储在一个临时变量中(使用“kong.ctx.plugin”对象),然后在所述插件的“日志”阶段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-14
      • 2020-04-30
      • 1970-01-01
      • 2021-12-06
      • 2021-09-12
      • 2022-06-11
      • 1970-01-01
      • 2018-10-27
      相关资源
      最近更新 更多