Capturing Response Body

Hi, I am using Kong 0.13 CE, I am trying to capture request body and response body for debugging purposes. So far I have managed to capture request body but not response body.

This is what I have tried so far, the lines commented out are one of the 2 ways that I have tested:

log_format log_req_resp escape=json '$remote_addr - $remote_user [$time_local] '
                        '"$request" $status $body_bytes_sent '
                        '"$http_referer" "$http_user_agent" $request_time req_body:"$request_body" resp_body:"$resp_body"';

server {
    server_name kong;
> for i = 1, #proxy_listeners do
    listen $(proxy_listeners[i].listener);
> end
    error_page 400 404 408 411 412 413 414 417 494 /kong_error_handler;
    error_page 500 502 503 504 /kong_error_handler;

   # access_log ${{PROXY_ACCESS_LOG}} postdata;
    access_log ${{PROXY_ACCESS_LOG}} log_req_resp;
    error_log ${{PROXY_ERROR_LOG}} ${{LOG_LEVEL}};

    lua_need_request_body on;

    set $resp_body "";
#    body_filter_by_lua '
#                       local resp_body = string.sub(ngx.arg[1], 1, 1000)
#                       ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
#                       if ngx.arg[2] then
#                               ngx.var.resp_body = ngx.ctx.buffered
#                       end
#                       ';
    body_filter_by_lua 'ngx.log(ngx.ERR, ngx.var.resp_body, ngx.var.request_body )';

Any ideas why I always get null resp_body?


Here is a sample of a guy that refactored udp/tcp plugins to give options to log req/resp bodies and have size limitations on it:

function TcpLogHandler:body_filter(conf)

  if conf.log_body and conf.max_body_size > 0 then
    local chunk = ngx.arg[1]
    local res_body = ngx.ctx.response_body .. (chunk or "")
    ngx.ctx.response_body = string.sub(res_body, 0, conf.max_body_size)

Full file example:

Thanks Jeremy I will have a look into this, just a question is it possible to write this data into the access logs? I mean I already have most of the data that we need in the access logs just would like to get the response body too.

Thanks :slight_smile: