Question

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?

Was it helpful?

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

OTHER TIPS

Have you ever tried netcat? To see if you have it do:

$ man nc

Then you can start a server like this:

$ nc -l 8080    (-l act as a server, listening on port 8080)
(hangs)

If I send a post request with the data 'a=10&b=20' to http://locahost:8080, netcat outputs:

$ nc -l 8080    
POST / HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:23.0) Gecko/20100101 Firefox/23.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: null
Accept-Encoding: gzip, deflate
DNT: 1
Content-Length: 9
Content-Type: text/plain; charset=UTF-8
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

a=10&b=20
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top