Search code examples
rubypostwebrick

WEBrick: log POST data


I'm running a simple WEBrick server to debug POST data. I'd like to output the POST data to the log.

My code is:

server.mount_proc '/' do |req, res|
    res.body = "Web server response:\n"
    # Output POST data here...
end

where server is simply a WEBrick server.

Any suggestions?


Solution

  • Access raw post data using req.body.

    server.mount_proc '/' do |req, res|
        res.body = "Web server response:\n"
        p req.body # <---
    end
    

    If you want parsed data (as hash), use req.query instead.

    UPDATE

    Customize :AccessLog:

    require 'webrick'
    
    log = [[ $stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT + ' POST=%{body}n']]
    
    server = WEBrick::HTTPServer.new :Port => 9000, :AccessLog => log
    server.mount_proc '/' do |req, res|
        req.attributes['body'] = req.body
        res.body = "Web server response:\n"
    end
    server.start